HDU - 3790 最短路径问题
Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
3 2 1 2 5 6 2 3 4 5 1 3 0 0
Sample Output
9 11
代码:

#include <iostream> #include<bits/stdc++.h> #define maxn 1007 #define INF 1<<30 using namespace std; int start,e; int n,m; int graph[maxn][maxn]; int cost[maxn][maxn]; int dist[maxn],cist[maxn],vis[maxn]; void Dijkstra(){ for(int i = 1;i <= n;i++) { dist[i] = graph[start][i]; cist[i] = cost[start][i]; } memset(vis,0,sizeof(vis)); vis[start] = 1; for(int i = 1;i <= n;i++){ if(vis[e]) break; int mindis = INF,mark; for(int j = 1;j <= n;j++) { if(!vis[j] &&dist[j]<mindis) mindis = dist[mark=j]; } vis[mark] = 1; for(int j = 1;j <= n;j++) { if(!vis[j]&&dist[j] > dist[mark]+graph[mark][j]) { dist[j] = dist[mark]+graph[mark][j]; cist[j] = cist[mark]+cost[mark][j]; } else if(!vis[j]&& dist[j] == dist[mark]+graph[mark][j]) if(cist[j] > cist[mark]+cost[mark][j]) cist[j] = cist[mark]+cost[mark][j]; } } printf("%d %d ",dist[e],cist[e]); } int main(){ while(scanf("%d%d",&n,&m) && n+m){ for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) { graph[i][j] = i==j?0:INF; cost[i][j] = i==j?0:INF; } int a,b,d,p; for(int i = 1;i <= m;i++){ scanf("%d%d%d%d",&a,&b,&d,&p); if(graph[a][b]>d){ graph[a][b]=graph[b][a]=d; cost[a][b]=cost[b][a]=p; } else if(graph[a][b]==d){ if(cost[a][b]>p) cost[a][b]=cost[b][a]=p; } } scanf("%d%d",&start,&e); Dijkstra(); } return 0; }