Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16141 Accepted Submission(s):
4859
(1<n<=1000, 0<m<100000, s != t)
#include<stdio.h> #include<string.h> #define max 1100 #define INF 0x3f3f3f int point; int begin,end; int lowdis[max],map[max][max],visit[max]; int lowcost[max]; int map1[max][max];//记录第i个点到其余所有点的花费 void dijkstra() { int mindis,next,j,i,mincost,l; memset(visit,0,sizeof(visit)); for(i=1;i<=point;i++) { lowdis[i]=map[begin][i]; lowcost[i]=map1[begin][i]; } visit[begin]=1; for(i=1;i<point;i++) { mindis=INF; for(j=1;j<=point;j++) { if(!visit[j]&&mindis>lowdis[j]) { mindis=lowdis[j]; next=j; } } visit[next]=1; for(j=1;j<=point;j++) { if(!visit[j]) { if(lowdis[j]==map[next][j]+lowdis[next])//这里要考虑两个点之间多条通路距离相等的问题 { if(lowcost[j]>map1[next][j]+lowcost[next]) { lowcost[j]=map1[next][j]+lowcost[next];//记下在通路距离相等情况下花费最少的 lowdis[j]=map[next][j]+lowdis[next]; } } else if(lowdis[j]>map[next][j]+lowdis[next])//两个点 只有一条通路 { lowdis[j]=map[next][j]+lowdis[next]; lowcost[j]=map1[next][j]+lowcost[next]; } } } } printf("%d %d\n",lowdis[end],lowcost[end]); } int main() { int n,m,a,b,d,p,j,i; while(scanf("%d%d",&point,&m)&&point!=0&&m!=0) { for(i=1;i<=point;i++) { for(j=1;j<=point;j++) { if(i==j) { map[i][j]=0; map1[i][j]=0; } else { map[i][j]=map[j][i]=INF; map1[i][j]=map1[j][i]=INF; } } } while(m--) { scanf("%d%d%d%d",&a,&b,&d,&p); if(map[a][b]>d) { map[a][b]=map[b][a]=d; map1[a][b]=map1[b][a]=p; } else if(map[a][b]==d&&map1[a][b]>p) //此题的输入就要加上两点之间可能有多条通路距离相等的数据 map1[a][b]=map1[b][a]=p; // } scanf("%d%d",&begin,&end); dijkstra(); } return 0; }