(最短路)hdu 3790
Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
(1<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0
Sample Output
9 11
这么水的题目WA到死,,,弱成翔啊啊啊啊,,贴大神代码
#include<iostream> #include<cstring> using namespace std; const int Maxn = 1000000; const int maxn = 10002; int dis[maxn][maxn], pis[maxn][maxn], d[maxn], p[maxn]; bool vis[maxn]; int n, m; void Distriaj(int s, int t) { for(int i = 1; i <= n; i++) { vis[i] = false; d[i] = dis[s][i]; p[i] = pis[s][i]; } vis[s] = true; for(int time = 1; time < n; time++) { int Min = Maxn, k = -1; for(int i = 1; i <= n; i++) if(!vis[i] && d[i]<Min) Min = d[i], k = i; vis[k] = true; for(int i = 1; i <= n; i++) { if(!vis[i] && d[i] > d[k]+dis[k][i]) { d[i] = d[k]+dis[k][i]; p[i] = p[k]+pis[k][i]; } if(!vis[i] && d[i]==d[k]+dis[k][i] && p[i] > p[k]+pis[k][i]) p[i] = p[k]+pis[k][i]; } } cout << d[t] << " " << p[t] << endl; } int main() { int s,t; while(cin >> n >> m ) { if(n==0&&m==0) break; for(int i = 1; i <= n; i++) { d[i] = maxn; p[i] = maxn; for(int j = 1; j <= n; j++) { if(i != j) { dis[i][j] = dis[j][i] = Maxn; pis[i][j] = pis[j][i] = Maxn; } else { dis[i][j] = 0; pis[i][j] = 0; } } } int a, b, c, d; for(int i = 1; i <= m; i++) { cin >> a >> b >> c >> d; if(dis[a][b] > c) { dis[a][b] = dis[b][a] = c; pis[a][b] = pis[b][a] = d; } if(dis[a][b] == c && pis[a][b] > d) pis[a][b] = pis[b][a] = d; } cin >> s>>t; Distriaj(s,t); } return 0; }