Minimum Transport Cost

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9109    Accepted Submission(s): 2405

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
 
题意:输入N个城市,然后接下来N*N表示相互之间的连接路径,接下来输入两个城市求最短路并输出路径
分析:首先用Floyd求任意两点的距离,然后在更新的时候更新path[][]
path[i][j]用来保存 i --> j 的最短路径中 i 的最优后驱(即最近),在Floyd三重循环时,一直更新path。  
在输出路径注意的就是相同的城市直接输入这个城市就可以了,这个问题上浪费了一个多小时。
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string.h>
using namespace std;
const int INF = << ;
const int MAX = ;
int n,path[MAX][MAX],dist[MAX][MAX],tax[MAX];
/path[i][j]用来保存 i --> j 的最短路径中 i 的最优后驱(即最近),在Floyd三重循环时,一直更新path。
void Floyd(int n)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
path[i][j] = j;
} for(int k = ; k <= n; k++)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
if(dist[i][k] < INF && dist[k][j] < INF)
{
int temp = dist[i][k] + dist[k][j] + tax[k];
if(temp < dist[i][j])
{
dist[i][j] = temp;
path[i][j] = path[i][k];
}
else if(temp == dist[i][j]) //因为是按照字典序输出路径因此当距离相等时,取路径中最小的那个
{
if(path[i][j] > path[i][k])
path[i][j] = path[i][k];
}
}
}
}
} }
int main()
{
while(scanf("%d", &n) != EOF && n)
{
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
scanf("%d", &dist[i][j]);
if(dist[i][j] == -)
dist[i][j] = INF;
}
}
for(int i = ; i <= n; i++)
scanf("%d", &tax[i]);
Floyd(n);
int start,goal;
while(scanf("%d%d", &start,&goal) != EOF)
{
if(start == - && goal == -)
break;
printf("From %d to %d :\n",start,goal); int temp = start;
printf("Path: %d",start);
while(temp != goal)
{
printf("-->%d", path[temp][goal]);
temp = path[temp][goal];
}
printf("\n");
/*
int temp = path[start][goal]; //就是这个一直把start和goal当成不会重复的了,脑子啊~
printf("Path: %d",start);
while(temp != goal)
{
printf("-->%d",temp);
temp = path[temp][goal];
}
printf("-->%d\n",goal);
*/
printf("Total cost : %d\n",dist[start][goal]);
printf("\n");
}
}
return ;
}
 

最新文章

  1. Java反射特性--获取其他类实例并调用其方法
  2. JavaWeb学习总结(十二)——Session
  3. 加密 bouncy castle
  4. Python Ogre Blender(转载)
  5. [转]ASP.NET MVC Jquery Validate 表单验证的多种方式介绍
  6. 解决wordpress的fonts.googleapis.com在国内无法访问的问题
  7. Shortest Prefixes(trie树唯一标识)
  8. HTTPS那个东西(一)-HTTPS原理
  9. SAP HANA 创建属性视图
  10. 百度,google的地理编码
  11. AES在线加密解密-附AES128,192,256,CBC,CFB,ECB,OFB,PCBC各种加密解密源码
  12. DSAPI 调用串口选择界面
  13. idea 排除编译 参考
  14. xcodebuild 错误:unknown error -1=ffffffffffffffff
  15. HTML页面本地正常,部署到服务器稍微异常解决方案
  16. VI编辑器常用命令
  17. 识别图片验证码的三种方式(scrapy模拟登陆豆瓣网)
  18. PHPer常见的面试题总结
  19. 【转】【CUBE】Oracle分组函数之CUBE魅力
  20. String 练习

热门文章

  1. Entity Framework版本历史概览
  2. IPAdr.exe破解[练手]
  3. [转]有关WorldWind1.4的worldwind.cs窗口设计器打开错误的解决方法
  4. 对EBS中配置文件的初步认识
  5. 如何在高并发分布式系统中生成全局唯一Id(转)
  6. LeetCode:Palindrome Partitioning,Palindrome Partitioning II
  7. TranslateAnimation 使用详解
  8. 对于RegExp反向引用的一点理解
  9. C# HttpWebRequest提交数据方式浅析
  10. jquery设置元素的readonly与diabled属性方法