标签:
Description
Input
Output
Sample Input
Sample Output
#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; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4243263.html