标签:des style blog http color os strong io
Description
Input
Output
Sample Input
Sample Output
#include <stdio.h> #include <string.h> #define ff 9999999; int dis[1002][2]; int vt[1002]; int s, t; //起点和终点 int n; //点的个数 struct N { int dd, p; }map[1002][1002]; void dijkstra() { int i, pos, k; int min; for(k=1; k<=n-1; k++ ) { pos=s; min=ff; for(i=1; i<=n; i++) { if(vt[i]==0 && dis[i][0] < min ) { pos=i; min = dis[i][0] ; } } vt[pos] = 1; for(i=1; i<=n; i++) { if(vt[i]==0 && dis[i][0] > dis[pos][0]+map[i][pos].dd ) { dis[i][0] = dis[pos][0] + map[i][pos].dd; dis[i][1] = dis[pos][1] + map[i][pos].p ; } else if(vt[i]==0 && dis[i][0] == dis[pos][0]+map[i][pos].dd ) { if(dis[i][1] > dis[pos][1] + map[i][pos].p ) { dis[i][0] = dis[pos][0] + map[i][pos].dd; dis[i][1] = dis[pos][1] + map[i][pos].p ; } } } } printf("%d %d\n", dis[t][0], dis[t][1] ); } int main() { int m; int i, j; int u, v; int d, gg; while(scanf("%d %d", &n, &m )!=EOF) { if(n==0 && m==0) break; memset(vt, 0, sizeof(vt)); for(i=1; i<=n; i++) //初始化结构体数组 { for(j=1; j<=n; j++) { if(i==j) { map[i][j].dd = 0; map[i][j].p =0; } else { map[i][j].dd = ff; map[i][j].p = ff; } } } for(j=0; j<m; j++) { scanf("%d %d %d %d", &u, &v, &d, &gg); if(map[u][v].dd > d) { map[u][v].dd = d; map[u][v].p = gg; map[v][u].dd = d; map[v][u].p = gg; } } scanf("%d %d", &s, &t); for(i=1; i<=n; i++) { dis[i][0] = map[s][i].dd ; dis[i][1] = map[s][i].p ; } vt[s] = 1; dijkstra(); } return 0; }
标签:des style blog http color os strong io
原文地址:http://www.cnblogs.com/yspworld/p/3884424.html