AcWing 1126. 最小花费 ( dijkstra||spfa)

整理的算法模板:ACM算法模板总结(分类详细版)

 

在 nn 个人中,某些人的银行账号之间可以互相转账。

这些人之间转账的手续费各不相同。

给定这些人之间转账时需要从转账金额里扣除百分之几的手续费,请问 AA 最少需要多少钱使得转账后 BB 收到 100 元。

输入格式

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

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

最后一行输入两个正整数 A,BA,B。

数据保证 AA 与 BB 之间可以直接或间接地转账。

输出格式

输出 AA 使得 BB 到账 100 元最少需要的总费用。

精确到小数点后 8 位。

数据范围

1≤n≤20001≤n≤2000,
m≤105m≤105

输入样例:

3 3
1 2 1
2 3 2
1 3 3
1 3

输出样例:

103.07153164
难度:简单
时/空限制:1s / 64MB
总通过数:420
总尝试数:832
来源:《信息学奥赛一本通》
算法标签  最短路

思路:

很明显可以把两个人之间的手续转化成树储存起来;从A到B,要想B最小,那么A到B路径上所扣除的手续费最小;可以从B==100倒推到A;每次找到和B相连,并且扣完手续费后得到B最小的x;可以用朴素版dijkstra以及dijkstra+heap或者spfa

y总朴素版dijkstra:

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

using namespace std;

const int N = 2010;

int n, m, S, T;
double 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]);
    }
}

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

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

    cin >> S >> T;

    dijkstra();

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

    return 0;
}

 

dijkstra+heap:

#include <bits/stdc++.h>
using namespace std;
typedef pair<double,int> PII;
const int N=200005;
int e[N],ne[N],h[N],idx,n,m;
double dis[N],w[N];
bool st[N];
void add(int a,int b,double c)
{
    e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
void dijkstra(int root)
{
    for(int i=0;i<N;i++) dis[i]=0x3f3f3f3f;
    priority_queue<PII,vector<PII>,greater<PII> > heap;
    heap.push({100,root});
    dis[root]=100;
    while(!heap.empty())
    {
        auto res=heap.top();
        heap.pop();
        auto ver=res.second;
        auto distance=res.first;
        if(st[ver]) continue;
        st[ver]=true;
        for(int i=h[ver];i!=-1;i=ne[i])
        {
            int j=e[i];
            if(dis[j]>distance/w[i])
            {
                dis[j]=distance/w[i];
                heap.push({dis[j],j});
            }
        }
    }
}
int main()
{
    memset(h,-1,sizeof h);
    cin >>n>>m;
    for(int i=0;i<m;i++)
    {
        int x,y,z;
        cin >>x>>y>>z;
        add(x,y,(100.0-z)/100);
        add(y,x,(100.0-z)/100);
    }
    int a,b;
    cin >>a>>b;
    dijkstra(b);
    printf("%.8lf",dis[a]);
}

 

spfa:

#include <bits/stdc++.h>
using namespace std;

const int M = 2000010, INF = 1000000000;
int n, m,A,B;
int h[M], e[M], ne[M], idx;       // 邻接表
double v[M],dist[M];
bool st[M];     // 存储每个点是否在队列中

void add(int a, int b, double c)
{
    e[idx] = b, v[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void spfa(int root)
{
    for (int i = 1; i <= n; i++) dist[i] = INF;
    dist[root]=100;
    queue<int> q;
    q.push(root), st[root] = true;
    while (!q.empty())
    {
        int t = q.front();
        q.pop();
        st[t] = false;
        for (int i = h[t]; i != -1; i = ne[i])
            if (dist[e[i]] > dist[t] / v[i])
            {
                dist[e[i]] = dist[t] / v[i];
                if (!st[e[i]])
                {
                    st[e[i]] = true;
                    q.push(e[i]);
                }
            }
    }
}

int main()
{
    memset(h, -1, sizeof h);
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, (100.0-c)/100);
        add(b, a, (100.0-c)/100);
    }
    cin >>A>>B;
    spfa(B);
    printf("%.8lf",dist[A]);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章