hdu1385
来源:互联网 发布:网络爬虫贴吧 编辑:程序博客网 时间:2024/05/21 10:32
Minimum Transport Cost
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10929 Accepted Submission(s): 3022
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
Source
Asia 1996, Shanghai (Mainland China)
Recommend
Eddy | We have carefully selected several similar problems for you: 1217 1690 2112 2722 1598
Statistic | Submit | Discuss | Note
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;const int N = 1234;const int INF = 12345678;int G[N][N];int dist[N];int vis[N];int pre[N];int b[N];int n;int h;void DFS_1(int k,char s[]){ if(pre[k] == -1) { return; } DFS_1(pre[k],s); s[h++]=k+'0';}void DFS_2(int k){ if(pre[k] == -1) { printf("%d",k); return; } DFS_2(pre[k]); printf("-->%d",k);}int cmp(int o,int p){ char str1[12345]; h=0; DFS_1(o,str1); str1[h]='\0'; char str2[12345]; h=0; DFS_1(p,str2); str2[h++]=o+'0';; str2[h]='\0'; if(strcmp(str1,str2) > 0) return 1; else return 0;}void dij(int v0,int s,int t){ int pos=v0; for(int i=1;i<=n;i++) { if(i==s || i==t) { dist[i]=G[v0][i]; } else { dist[i]=G[v0][i]+b[i]; } if(i != v0 && G[v0][i] < INF) { pre[i]=v0; } } vis[pos]=1; for(int i=1;i<n;i++) { int mins=INF; for(int j=1;j<=n;j++) { if(!vis[j] && dist[j] < mins) { pos=j; mins=dist[j]; } } vis[pos]=1; for(int j=1;j<=n;j++) { if(!vis[j]) { int f; if(j==s || j==t) { f=dist[pos]+G[pos][j]; } else { f=dist[pos]+G[pos][j]+b[j]; } if(dist[j] > f) { pre[j]=pos; dist[j] = f; } else if(dist[j] == f) { if(cmp(j,pos)) { pre[j]=pos; } } } } }}int main(){ while(scanf("%d",&n),n) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { scanf("%d",&G[i][j]); if(G[i][j]==-1) { G[i][j]=INF; } } } for(int i=1;i<=n;i++) { scanf("%d",&b[i]); } int u,v; while(~scanf("%d %d",&u,&v)) { if(u==-1 && v==-1) { break; } memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); dij(u,u,v); printf("From %d to %d :\n",u,v); printf("Path: "); DFS_2(v); puts(""); printf("Total cost : "); printf("%d\n",dist[v]); puts(""); } } return 0;}
- Hdu1385
- hdu1385
- hdu1385
- HDU1385
- HDU1385
- hdu1385
- hdu1385
- HDU1385 period
- HDU1385-Floyed
- Floyd hdu1385
- hdu1385 Minimum Transport Cost
- hdu1385 flody+记录路径
- HDU1385 Minimum Transport Cost
- HDU1385 Minimum Transport Cost
- Minimum Transport Cost HDU1385
- HDU1385 Floyd加正序记录路径
- ZOJ1456 HDU1385 Minimum Transport Cost,Dijkstra算法
- hdu1385(floyd算法+最短路径)
- 关于抽象类和接口的异同
- myflag初期准备工作
- 点云TXT转化为pcd格式
- 因特网五层协议栈以及OSI七层协议模型
- 从源程序到可执行程序详细过程及unix环境下具体过程(程序员须知)
- hdu1385
- 198. House Robber LeetCode
- 算法设计与应用基础:第十一周(1)
- 拓扑排序-java
- 列举全排列的递归算法的java代码实现
- 文章标题
- 一分钟教你破解intelliJ idea 2017
- JAVA 中的匿名内部类总结
- 10小时之内,暴力破解SSH账号的IP