标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22854 Accepted Submission(s):
6813
#include <cstring> #include <stdio.h> #include <algorithm> using namespace std; #define maxn 1010 #define INF 0x3f3f3f3f int dis[maxn],visit[maxn],cost[maxn]; int maps[maxn][maxn],maps2[maxn][maxn]; int n,m; int start,send; void dijkstra(int s) { for(int i=1; i<=n; i++) { dis[i]=maps[start][i]; cost[i]=maps2[start][i]; visit[i]=0; } visit[s]=1; dis[s] = 0; cost[s] = 0; for(int i=0; i<n-1; i++) { int temp=INF, k =0; for(int j=1; j<=n; j++) { if(visit[j]==false) { if(temp>dis[j]) { temp = dis[j]; k = j; } } } visit[k]=1; for(int j=1; j<=n; j++) { if(!visit[j]) { if(dis[j]>dis[k]+maps[k][j]) { dis[j]=dis[k]+maps[k][j]; cost[j]=cost[k]+maps2[k][j]; } else if(dis[j]==dis[k]+maps[k][j]) cost[j]=min(cost[j],cost[k]+maps2[k][j]); } } } } int main() { //freopen("input.txt","r",stdin); while(scanf("%d%d",&n,&m),n) { for(int i=1; i<=n; ++i) { for(int j=1; j<=n; ++j) { if(i==j) maps[i][j]=maps2[i][j]=0; else maps[i][j]=maps2[i][j]=INF; } } while(m--) { int a,b,d,c; scanf("%d%d%d%d",&a,&b,&d,&c); if(maps[a][b]>d) { maps[a][b]=maps[b][a]=d; maps2[a][b]=maps2[b][a]=c; } if(maps[a][b]==d&&maps2[a][b]>c) maps2[a][b]=maps2[b][a]=c; } scanf("%d%d",&start,&send); dijkstra(start); printf("%d %d\n",dis[send],cost[send]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/TreeDream/p/5748856.html