整理的算法模板: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;
}