最小花费

// 最小花费.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
 
/*
//http://ybt.ssoier.cn:8088/problem_show.php?pid=1344
https://www.luogu.com.cn/problem/P1576

【题目描述】
在n个人中,某些人的银行账号之间可以互相转账。这些人之间转账的手续费各不相同。给定这些人之间转账时需要从转账金额里扣除百分之几的手续费,请问A最少需要多少钱使得转账后B收到100元。

【输入】
第一行输入两个正整数n,m,分别表示总人数和可以互相转账的人的对数。

以下m行每行输入三个正整数x,y,z,表示标号为x的人和标号为y的人之间互相转账需要扣除z%的手续费 (z<100)。

最后一行输入两个正整数A,B。数据保证A与B之间可以直接或间接地转账。

【输出】
输出A使得B到账100元最少需要的总费用。精确到小数点后8位。

【输入样例】
3 3
1 2 1
2 3 2
1 3 3
1 3
【输出样例】
103.07153164
【提示】
【数据规模】

1<=n<=2000
m<=10^5


3 6
1 2 2
2 1 3
2 3 2
3 2 5
1 3 4
3 1 8
1 3
*/

//以两个之间的兑换率为路径 进行转换 startw1w3*wx = en 求最大值
那么 100/dist[en] 就是为了终点得到100 ,起点的最小起始值

路径值是double的写法

#include <iostream>
#include <queue>
#include <cstring>

using  namespace std;
typedef pair<double, int> PII;

const int N = 2100;
const int M = 200010;
int h[N], e[M], ne[M], idx;
double w[M];
double dist[N];
bool st[N];     // 存储每个点的最短距离是否已确定
int n, m;
int start, en;

void add(int a, int b, double c) {
	e[idx] = b;
	w[idx] = c;
	ne[idx] = h[a];
	h[a] = idx++;
}	

// 求1号点到n号点的最短距离,如果不存在,则返回-1
double dijkstra()
{
	for (int i = 0; i < N; i++) dist[i] = 0;
    dist[start] = 1.0;
    priority_queue<PII, vector<PII>, less<PII>> heap;
    heap.push({ 1.0,start });      // first存储距离,second存储节点编号

    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.second; double distance = t.first;

        if (st[ver]) continue;
        st[ver] = true;

        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] <  distance * w[i]  )
            {
                dist[j] = distance * w[i]   ;
                heap.push({ dist[j], j });
            }
        }
    }

    return dist[en];
}


 

int main()
{
	memset(h, -1, sizeof h);	

	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
        int a, b, c; cin >> a >> b >> c;
		add(a, b, 1.0*(100-c)/100); add(b, a, 1.0 * (100 - c) / 100);
	}
    cin >> start >> en;
  
    dijkstra();

    printf("%.8lf\n", 100 / dist[en]);

	return 0;
}

路径值是100-c的写法

#include <iostream>
#include <queue>
#include <cstring>

using  namespace std;
typedef pair<double, int> PII;

const int N = 2100;
const int M = 200010;
int h[N], e[M], w[M], ne[M], idx;
double dist[N];
bool st[N];     // 存储每个点的最短距离是否已确定
int n, m;
int start, en;

void add(int a, int b, int c) {
	e[idx] = b;
	w[idx] = c;
	ne[idx] = h[a];
	h[a] = idx++;
}	

// 求1号点到n号点的最短距离,如果不存在,则返回-1
double dijkstra()
{
	for (int i = 0; i < N; i++) dist[i] = 0;
    dist[start] = 1.0;
    priority_queue<PII, vector<PII>, less<PII>> heap;
    heap.push({ 1.0,start });      // first存储距离,second存储节点编号

    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.second; double distance = t.first;

        if (st[ver]) continue;
        st[ver] = true;

        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] <  distance * 1.0*w[i]/100.0 )
            {
                dist[j] = distance * 1.0*w[i] / 100.0 ;
                heap.push({ dist[j], j });
            }
        }
    }

    return dist[en];
}


 

int main()
{
	memset(h, -1, sizeof h);	

	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
        int a, b, c; cin >> a >> b >> c;
		add(a, b, 100-c); add(b, a, 100-c);
	}
    cin >> start >> en;
  
    dijkstra();

    printf("%.8lf\n", 100 / dist[en]);

	return 0;
}

未使用堆优化的dijkstra

#define  _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 2010;

int n, m, S, T;
int g[N][N];
double dist[N];
bool st[N];

void dijkstra()
{
    dist[S] = 1;
    for (int i = 1; i <= n; i++)
    {
        int t = -1;
        for (int j = 1; j <= n; j++)
            if (!st[j] && (t == -1 || dist[t] < dist[j]))
                t = j;
        st[t] = true;

        for (int j = 1; j <= n; j++)
            dist[j] = max(dist[j], dist[t] * g[t][j]/100.0);
    }
}

int main()
{
    scanf("%d%d", &n, &m);

    while (m--)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        int z = (100.0 - c);
        g[a][b] = g[b][a] = max(g[a][b], z);
    }

    cin >> S >> T;

    dijkstra();

    printf("%.8lf\n", 100 / dist[T]);

    return 0;
}

posted on 2025-04-09 15:46  itdef  阅读(42)  评论(0)    收藏  举报

导航