POJ 1385

简介: Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4487    Accepted Submi...

Minimum Transport Cost

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


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
 1 //转载 
 2 //路径存储的是前一个点的后继 
 3 #include <iostream>
 4 #include <stdio.h>
 5 #include <memory.h>
 6 #include <queue>
 7 using namespace std;
 8 const int INF = 0x7fffff;//并不是整形最大值,防止溢出 
 9 const int N = 10000;
10 int map[N][N], tax[N], path[N][N];
11 int n;
12 void init()
13 {
14     int i, j;
15     for(i = 1; i <= n; i++)
16         for(j = 1; j <= n; j++)
17             if(i == j) map[i][j] = 0;
18             else map[i][j] = INF;
19 }
20 void input()
21 {
22     int i, j, k;
23     for(i = 1; i <= n; i++)
24         for(j = 1; j <= n; j++)
25         {
26             scanf("%d", &k);
27             if(k != -1) map[i][j] = k;
28             path[i][j] = j;
29         }
30     for(i = 1; i <= n; i++)
31         scanf("%d", &tax[i]);
32 }
33 void floyd()
34 {
35     int i, j, k, len;
36     for(k = 1; k <= n; k++)
37     {
38         for(i = 1; i <= n; i++)
39         {
40             for(j = 1; j <= n; j++)
41             {
42                 len = map[i][k] + map[k][j] + tax[k];
43                 if(map[i][j] > len)
44                 {
45                     map[i][j] = len;
46                     path[i][j] = path[i][k];    //标记到该点的前一个点
47                 }
48                 else if(len == map[i][j])   //若距离相同
49                 {
50                     if(path[i][j] > path[i][k]) //判断是否为字典顺序
51                         path[i][j] = path[i][k];
52                 }
53             }
54         }
55     }
56 }
57 void output()
58 {
59     int i, j, k;
60     while(scanf("%d %d", &i, &j))
61     {
62         if(i == -1 && j == -1) break;
63         printf("From %d to %d :\n", i, j);
64         printf("Path: %d", i);
65         k = i;
66         while(k != j)   //输出路径从起点直至终点
67         {
68             printf("-->%d", path[k][j]);
69             k = path[k][j];
70         }
71         printf("\n");
72         printf("Total cost : %d\n\n", map[i][j]);
73     }
74 }
75 int main()
76 {
77     while(scanf("%d", &n), n)
78     {
79         init();
80         input();
81         floyd();
82         output();
83     }
84 
85     return 0;
86 }
87 /*
88 C语言,二维数组申请 
89 int **map;
90 *map = (int *)malloc(sizeof(int)*(city));
91 for(i=0;i<city;i++)
92     map[i] = (int *)malloc(sizeof(int)*city);
93 */

 

目录
相关文章
|
消息中间件 存储 Kafka
深入解析Kafka中的动态更新模式
深入解析Kafka中的动态更新模式
210 0
|
关系型数据库 MySQL 网络安全
|
机器学习/深度学习 监控 自动驾驶
深度学习中的2D目标检测
2D目标检测是深度学习中的一个关键任务,旨在识别图像中的目标对象,并在每个目标对象周围生成一个边界框。该任务在自动驾驶、视频监控、机器人视觉等领域具有广泛应用。
207 5
|
Java 关系型数据库 MySQL
浪漫编码:手把手教你实现校园表白墙功能
浪漫编码:手把手教你实现校园表白墙功能
217 0
|
消息中间件 Linux
mq报错abbit@syld36: * connected to epmd (port 4369) on syld36 * epmd reports node ‘rabbit‘ uses po
mq报错abbit@syld36: * connected to epmd (port 4369) on syld36 * epmd reports node ‘rabbit‘ uses po
242 0
|
移动开发 前端开发 JavaScript
ruoyi-nbcio-plus的Vue3前端升级组件后出现的问题(一)
ruoyi-nbcio-plus的Vue3前端升级组件后出现的问题(一)
801 2
|
SQL 消息中间件 关系型数据库
Flink报错问题之cdc任务报错如何解决
Apache Flink是由Apache软件基金会开发的开源流处理框架,其核心是用Java和Scala编写的分布式流数据流引擎。本合集提供有关Apache Flink相关技术、使用技巧和最佳实践的资源。
|
Ubuntu 网络协议 Linux
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
Linux(20) Ubuntu 20.04 网络接口自动切换路由配置
870 0
|
数据采集
指标体系构建-02-从0开始,梳理数据指标体系
指标体系构建-02-从0开始,梳理数据指标体系
|
JavaScript 前端开发
vue表格合计 计算 保留两位小数
vue表格合计 计算 保留两位小数