hdu 题目1385 Minimum Transport Cost (最短路径,路径保存及筛选)
2013-08-20 21:35
309 查看
Minimum Transport Cost
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 19 Accepted Submission(s) : 7
Problem Description
These are N cities in Spring country. Between each pair of cities there may be one transportation track or none. Now there is some cargo that should be delivered from one city to another. The transportation fee consists of two parts:The cost of the transportation on the path between these cities, and
a certain tax which will be charged whenever any cargo passing through one city, except for the source and the destination cities.
You must write a program to find the route which has the minimum cost.
Input
First is N, number of cities. N = 0 indicates the end of input.The data of path cost, city tax, source and destination cities are given in the input, which is of the form:
a11 a12 ... a1N
a21 a22 ... a2N
...............
aN1 aN2 ... aNN
b1 b2 ... bN
c d
e f
...
g h
where aij is the transport cost from city i to city j, aij = -1 indicates there is no direct path between city i and city j. bi represents the tax of passing through city i. And the cargo is to be delivered from city c to city d, city e to city f, ..., and
g = h = -1. You must output the sequence of cities passed by and the total cost which is of the form:
Output
From c to d :Path: c-->c1-->......-->ck-->d
Total cost : ......
......
From e to f :
Path: e-->e1-->..........-->ek-->f
Total cost : ......
Note: if there are more minimal paths, output the lexically smallest one. Print a blank line after each test case.
Sample Input
5 0 3 22 -1 4 3 0 5 -1 -1 22 5 0 9 20 -1 -1 9 0 4 4 -1 20 4 0 5 17 8 3 1 1 3 3 5 2 4 -1 -1 0
Sample Output
From 1 to 3 : Path: 1-->5-->4-->3 Total cost : 21 From 3 to 5 : Path: 3-->4-->5 Total cost : 16 From 2 to 4 : Path: 2-->1-->5-->4 Total cost : 17
需要注意保存路径时,当当有两个最短路径冲突时,要进行筛选路径
最短路径用的Dijkstra 算法,
#include<iostream> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #define maxnum 10000 #define int_max 999999 using namespace std; int dist[maxnum]; int prev[maxnum]; int b[maxnum]; int c[maxnum][maxnum]; int n,can; int com(int cur,int v,int a,int b)//筛选路径 { int path1[maxnum]; int path2[maxnum]; path1[0]=cur; path2[0]=cur; int i=2,j=2; path1[1]=a; path2[1]=b; while(a!=v) { path1[i++]=prev[a]; a = prev[a]; } while(b!=v) { path2[j++]=prev[b]; b= prev[b]; } while(1) { i--,j--; if(i<0) return path1[1]; else if(j<0) return path2[1]; if(path1[i]<path2[j])return path1[1]; else if(path1[i]>path2[j]) return path2[1]; } } void Dijkstra(int n,int v,int t,int *dist,int *prev,int c[maxnum][maxnum],int *b) { bool s[maxnum]; for(int i=1;i<=n;i++) { s[i] = 0; dist[i] = c[v][i]; if(dist[i]==int_max) prev[i]=0; else prev[i]=v; } dist[v] =0; s[v] =1; for(int i=1;i<=n;i++) { int min = int_max; int u = v; for(int j=1;j<=n;j++) { if(!s[j]&&dist[j]<min) { min = dist[j]; u = j; } } s[u]=1; for(int j=1;j<=n;j++) { if(c[u][j]==int_max)continue; int newint=b[u]+dist[u]+c[u][j]; if(!s[j]&&newint<dist[j]) { dist[j]=newint; prev[j]= u; } else if(!s[j]&&newint==dist[j]) prev[j]= com(j,v,prev[j],u); } } int path[maxnum]; int k = 1; path[k++]=t; int tmp = prev[t]; while(tmp != v){ path[k++] = tmp; tmp = prev[tmp]; } path[k] = v; for(int i=k ;i>1;i--){ printf("%d-->",path[i]); }printf("%d\n",path[1]); } int main() { int i,j,ss,t,r; while(scanf("%d",&n),n) { for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ scanf("%d",&r); if(r==-1){ c[i][j] =int_max; } else c[i][j] = r; } } for(i=1;i<=n;i++) scanf("%d",&b[i]); while(scanf("%d%d",&ss,&t),(ss!=-1&&t!=-1)){ printf("From %d to %d :\nPath: ",ss,t); if(ss==t) { printf("%d\nTotal cost : 0\n\n",ss); } else{ Dijkstra(n,ss,t,dist,prev,c,b); printf("Total cost : %d\n\n",dist[t]); } } } return 0; }
相关文章推荐
- hdu 1385 Minimum Transport Cost(输出最短路径)
- hdu 1385(求出最短路并输出最短路径)
- HDU 1385 Minimum Transport Cost 最短路径题解
- hdu 1385 spfa和floyd,dijkstra记录最短最小字典序的路径
- hdu 题目1596 find the safest road (最短路径 dijkstra)
- hdu 1385 Minimum Transport Cost (最小字典序最短路径)
- hdu 3790 最短路径&最低费用 题目不难,但要注意重边
- HDU 1385 Minimum Transport Cost (Floyd求最短路径+记录字典序路径)
- hdu 题目2122 HDU Today (最短路径,Dijkstra)
- hdu 1385 Minimum Transport Cost 最短路径floyd算法+路径记录 模板题
- Floyd算法——保存路径——输出路径 HDU1385
- HDU 1385 Minimum Transport Cost (floyd算法,求最短路径)
- TOJ 1837 HDU 1385 ZOJ 1456 Minimum Transport Cost / 最短路径
- 【最短路径-Floyd+路径】hdu 1385
- HDU 1385 Minimum Transport Cost(多源最短路径+路径记录)
- hdu 1385 最短路径按字典数输出
- Minimum Transport Cost hdu 1385(最短路 + 路径保存 + stack)
- HDU 1385 Minimum Transport Cost 最短路径题解
- hdu-1385 Minimum Transport Cost(Dijkstra最短路径算法)
- hdu 3790 最短路径模版