Problem Description
Give you n points, m undirected edges, each side has length d and cost p, give you the starting point s end point t, require the shortest distance from the starting point to the end point and its cost, if the shortest distance has multiple routes, then the output cost is the least.
Input
Input n,m, point number is 1~n, followed by M lines, each line has four numbers a,b,d,p, indicating that there is a n edge between a a n d b, a n d its length is d, the cost is p. The last line is two numbers s,t; starting point s, ending point. The input ends when n and m are 0.
(1<n<=1000, 0<m<100000, s != t)
Output
The output line has two numbers, the shortest distance and its cost.
Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0
Sample Output
9 11
This problem can be solved by finding the shortest path, but there may be many roads in two places, so we should first find out the shortest distance between two places, and then find out the least cost when the distance is the same.
#include<stdio.h> #include<string.h> #define N 1020 int e[N][N],cos[N][N],book[N],dis[N],cs[N]; int main() { int a,b,d,p,i,j,k,n,m,s,t,min,u,v; int intf=99999999; while(scanf("%d %d",&n,&m)!=EOF) { if(m==0&&n==0) break; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(i==j) e[i][j]=cos[i][j]=0; else e[i][j]=e[j][i]=cos[i][j]=cos[j][i]=intf; for(i=0;i<m;i++) { scanf("%d %d %d %d",&a,&b,&d,&p); if(d<e[a][b]) { e[a][b]=e[b][a]=d; cos[a][b]=cos[b][a]=p; } if(d==e[a][b]) cos[a][b]=cos[b][a]=p; } scanf("%d %d",&s,&t); for(i=1;i<=n;i++) book[i]=0; book[s]=1; for(i=1;i<=n;i++) { dis[i]=e[s][i]; cs[i]=cos[s][i]; } for(i=1;i<=n-1;i++) { min=intf; for(j=1;j<=n;j++) { if(book[j]==0&&dis[j]<min) { min=dis[j]; u=j; } } book[u]=1; for(v=1;v<=n;v++) { if(e[u][v]<intf) { if(dis[v]>dis[u]+e[u][v]) { dis[v]=dis[u]+e[u][v]; cs[v]=cs[u]+cos[u][v]; } if(dis[v]==dis[u]+e[u][v]&&cs[v]>cs[u]+cos[u][v]) cs[v]=cs[u]+cos[u][v]; } } } printf("%d %d\n",dis[t],cs[t]); } return 0; }