博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HD1385Minimum Transport Cost(Floyd + 输出路径)
阅读量:5128 次
发布时间:2019-06-13

本文共 4783 字,大约阅读时间需要 15 分钟。

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。  
在输出路径注意的就是相同的城市直接输入这个城市就可以了,这个问题上浪费了一个多小时。
1 #include 
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 const int INF = 1 << 25; 8 const int MAX = 1000; 9 int n,path[MAX][MAX],dist[MAX][MAX],tax[MAX];10 /path[i][j]用来保存 i --> j 的最短路径中 i 的最优后驱(即最近),在Floyd三重循环时,一直更新path。 11 void Floyd(int n)12 {13 for(int i = 1; i <= n; i++)14 {15 for(int j = 1; j <= n; j++)16 path[i][j] = j;17 }18 19 for(int k = 1; k <= n; k++)20 {21 for(int i = 1; i <= n; i++)22 {23 for(int j = 1; j <= n; j++)24 {25 if(dist[i][k] < INF && dist[k][j] < INF)26 {27 int temp = dist[i][k] + dist[k][j] + tax[k];28 if(temp < dist[i][j])29 {30 dist[i][j] = temp;31 path[i][j] = path[i][k];32 }33 else if(temp == dist[i][j]) //因为是按照字典序输出路径因此当距离相等时,取路径中最小的那个34 {35 if(path[i][j] > path[i][k])36 path[i][j] = path[i][k];37 }38 }39 }40 }41 }42 43 44 }45 int main()46 {47 while(scanf("%d", &n) != EOF && n)48 {49 for(int i = 1; i <= n; i++)50 {51 for(int j = 1; j <= n; j++)52 {53 scanf("%d", &dist[i][j]);54 if(dist[i][j] == -1)55 dist[i][j] = INF;56 }57 }58 for(int i = 1; i <= n; i++)59 scanf("%d", &tax[i]);60 Floyd(n);61 int start,goal;62 while(scanf("%d%d", &start,&goal) != EOF)63 {64 if(start == -1 && goal == -1)65 break;66 printf("From %d to %d :\n",start,goal);67 68 int temp = start;69 printf("Path: %d",start);70 while(temp != goal)71 {72 printf("-->%d", path[temp][goal]);73 temp = path[temp][goal];74 }75 printf("\n");76 /*77 int temp = path[start][goal]; //就是这个一直把start和goal当成不会重复的了,脑子啊~78 printf("Path: %d",start);79 while(temp != goal)80 {81 printf("-->%d",temp);82 temp = path[temp][goal];83 }84 printf("-->%d\n",goal); 85 */86 printf("Total cost : %d\n",dist[start][goal]);87 printf("\n");88 }89 }90 return 0;91 }

 

 

 

转载于:https://www.cnblogs.com/zhaopAC/p/4990040.html

你可能感兴趣的文章
包含列的索引:SQL Server索引的阶梯级别5
查看>>
myeclipse插件安装
查看>>
浙江省第十二届省赛 Beauty of Array(思维题)
查看>>
NOIP2013 提高组 Day1
查看>>
cocos2dx 3.x simpleAudioEngine 长音效被众多短音效打断问题
查看>>
存储(硬件方面的一些基本术语)
查看>>
观察者模式
查看>>
Weka中数据挖掘与机器学习系列之基本概念(三)
查看>>
Win磁盘MBR转换为GUID
查看>>
大家在做.NET B/S项目的时候多用什么设技术啊?
查看>>
Java SE和Java EE应用的性能调优
查看>>
Android设计模式系列--原型模式
查看>>
免费的论文查重网站
查看>>
C语言程序第一次作业
查看>>
leetcode-Sort List
查看>>
中文词频统计
查看>>
了解node.js
查看>>
想做移动开发,先看看别人怎么做
查看>>
Eclipse相关集锦
查看>>
虚拟化架构中小型机构通用虚拟化架构
查看>>