hdu 1385 Floyd 输出路径

Floyd 输出路径

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

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <string>
 5 # include <algorithm>
 6 # include <cmath>
 7 # include <map>
 8 # define LL long long
 9 using namespace std ;
10
11 const int MAXN = 310 ;
12 const int INF = 0x3f3f3f3f;
13 int dis[MAXN][MAXN];
14 int b[MAXN] ;
15 int path[MAXN][MAXN] ;
16 int n ;
17
18 void floyed()//节点从1~n编号
19 {
20     int i,j,k;
21     for (i = 1; i <= n; i++)
22         for (j = 1; j <= n; j++)
23             path[i][j] = j;  //记录路径数组初始化,表示从i到j经过的第一个站
24     for(k=1;k<=n;k++)
25        for(i=1;i<=n;i++)
26          for(j=1;j<=n;j++)
27          {
28              int t = dis[i][k]+dis[k][j] + b[k] ;
29              if (t < dis[i][j])
30              {
31                  dis[i][j] = t ;
32                  path[i][j] = path[i][k] ;
33              }
34              else if (t == dis[i][j] && path[i][k] < path[i][j])
35                  path[i][j] = path[i][k] ;
36
37          }
38
39
40 }
41
42 int main()
43 {
44    // freopen("in.txt","r",stdin) ;
45     while (scanf("%d" , &n) , n)
46     {
47         int i , j ;
48         for (i = 1 ; i <= n ; i++)
49             for (j = 1 ; j <= n ; j++)
50         {
51             scanf("%d" , &dis[i][j]) ;
52             if (dis[i][j] == -1)
53                 dis[i][j] = INF ;
54         }
55         for (i = 1 ; i <= n ; i++)
56             scanf("%d" , &b[i]) ;
57         floyed() ;
58         int u , v ;
59         while(scanf("%d %d" , &u , &v) )
60         {
61             if (u == -1 && v == -1)
62                 break ;
63             printf ("From %d to %d :\n", u, v);
64             printf ("Path: %d", u);
65             int t = u;
66             while (u != v)
67             {
68                 printf ("-->%d", path[u][v]);
69                 u = path[u][v];
70             }
71             printf ("\nTotal cost : %d\n\n", dis[t][v]);
72         }
73
74     }
75     return 0;
76 }

时间: 2024-08-03 22:03:46

hdu 1385 Floyd 输出路径的相关文章

HD1385Minimum Transport Cost(Floyd + 输出路径)

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

(floyd输出路径) hdu 1385

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8085    Accepted Submission(s): 2105 Problem Description These are N cities in Spring country. Between each pair of cities

hdu 1503 LCS输出路径【dp】

hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左走.我用1表示向上走,2向左上方走,3向左走. 刚开始输入字符串时有两种方法,直接输入:或从地址后一位输入,即此时数组起始编号为1.直接输入时,dp[i][j]表示的是以s1[i-1],s2[j-1]为结尾LCS,另一种则就是表示以s1[i],s2[j]为结尾的LCS.两者在路径输出时有些差别,以前

*HDU 1385 最短路 路径

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10183    Accepted Submission(s): 2791 Problem Description These are N cities in Spring country. Between each pair of cities

HDU 1385 Minimum Transport Cost(Floyd 最短路 打印路径)

HDU 1385 大意: 有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度. 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费). 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和. 求最小花费,如果有多条路经符合,则输出字典序最小的路径. 思路: Floyd求最短路,打印路径即可. 1 /*--------------------------------------------------------

hdu 1160 FatMouse&#39;s Speed(最长不下降子序列+输出路径)

题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the s

hdu Minimum Transport Cost(按字典序输出路径)

 摘自:Think In Java 从技术角度说,OOP(面向对象程序设计)只是涉及抽象的数据类型.继承以及多形性,但另一些问题也可能显得非常重要.本节将就这些问题进行探讨.最重要的问题之一是对象的创建及破坏方式.对象需要的数据位于哪儿,如何控制对象的"存在时间"呢?针对这个问题,解决的方案是各异其趣的.C++认为程序的执行效率是最重要的一个问题,所以它允许程序员作出选择.为获得最快的运行速度,存储以及存在时间可在编写程序时决定,只需将对象放置在堆栈(有时也叫作自动或定域变量)或者静态

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14624    Accepted Submission(s): 4634 Special Judge Problem Description The Princess has been abducted by the BEelzeb

hdu 1026 Ignatius and the Princess I(bfs搜索+输出路径)

题目来源:hdu-1026 Ignatius and the Princess I Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14677 Accepted Submission(s): 4653 Special Judge Problem Description The Princ