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 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
 
题意:
从一个城市到另一个城市有油费和每经过一个城市要缴纳过路费,起点和终点不用交,问从起点到终点最少的费用。并打印路径。
代码:
 //floyd 用一个path数组记录路径。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int mp[][];
int fei[],path[][];
int main()
{
int n,a,b,t;
while(scanf("%d",&n)&&n)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==-)
mp[i][j]=;
path[i][j]=j; //初始化路径
}
for(int i=;i<=n;i++)
scanf("%d",&fei[i]);
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(mp[i][j]>mp[i][k]+mp[k][j]+fei[k]) //加上tax.
{
mp[i][j]=mp[i][k]+mp[k][j]+fei[k];
path[i][j]=path[i][k];
}
else if(mp[i][j]==mp[i][k]+mp[k][j]+fei[k]&&path[i][j]>path[i][k])
path[i][j]=path[i][k];
}
while(scanf("%d%d",&a,&b))
{
if(a<&&b<) break;
printf("From %d to %d :\n",a,b);
int x=a;
printf("Path: %d",a);
while(x!=b)
{
printf("-->%d",path[x][b]);
x=path[x][b];
}
printf("\n");
printf("Total cost : %d\n\n",mp[a][b]);
}
}
return ;
}

*HDU 1385 最短路 路径的更多相关文章

  1. hdu 1385 Floyd 输出路径

    Floyd 输出路径 Sample Input50 3 22 -1 43 0 5 -1 -122 5 0 9 20-1 -1 9 0 44 -1 20 4 05 17 8 3 1 //收费1 3 // ...

  2. hdu 1385 floyd记录路径

    可以用floyd 直接记录相应路径 太棒了! http://blog.csdn.net/ice_crazy/article/details/7785111 #include"stdio.h& ...

  3. hdu 1385(Floyed+打印路径好题)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  5. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  7. 堆优化Dijkstra计算最短路+路径计数

    今天考试的时候遇到了一道题需要路径计数,然而蒟蒻从来没有做过,所以在考场上真的一脸懵逼.然后出题人NaVi_Awson说明天考试还会卡SPFA,吓得我赶紧又来学一波堆优化的Dijkstra(之前只会S ...

  8. CodeForces - 449B 最短路(迪杰斯特拉+堆优化)判断最短路路径数

    题意: 给出n个点m条公路k条铁路. 接下来m行 u v w      //u->v 距离w 然后k行 v w         //1->v 距离w 如果修建了铁路并不影响两点的最短距离, ...

  9. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

随机推荐

  1. 在Heroku部署时,无法加载 css,js,图片资源解决办法

    解决方案: 首先查看Gemfile, 确保group :production do 里添加了 gem "rails_12factor", '0.0.2' 然后在本地执行 rails ...

  2. Unix目录结构的来历

    作者: 阮一峰 Unix(包含Linux)的初学者,常常会很困惑,不明白目录结构的含义何在. 举例来说,根目录下面有一个子目录/bin,用于存放二进制程序.但是,/usr子目录下面还有/usr/bin ...

  3. Windows环境配置HTTP服务(Windows + Apache + Mysql + PHP)

    1.安装WampServer 2.管理HTTP服务 任务图标绿色为正常启动状态 注意事项:1.检查网络是不是通的 ping 对方IP2.检查防火墙是否开启,如果开启将不能正常被访问3.检查访问权限 A ...

  4. MySQL SQL中的安全问题

    一.SQL注入是一个很常见的问题:利用的原理是SQL语句中的 "or"操作符或者"/*"和"#"注释符.前者利用逻辑运算,或者利用MySQL ...

  5. Python自动化之一对多

    一对多 建立一对多关系之后(也就是加了外键),会在字表里面多一个"外键字段_id"这个字段 查询 #_*_coding:utf-8_*_ from django.db import ...

  6. python运算符

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcIAAAHCCAIAAADzel4SAAAgAElEQVR4Aey9+bMcSXLnV1dmna/ejR

  7. SET基本数据类型

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO4AAADZCAIAAACo85tgAAAgAElEQVR4Aey9SdAs13XnV/P8jW8e8D

  8. java19

    1:异常(理解) (1)程序出现的不正常的情况. (2)异常的体系 Throwable |--Error 严重问题,我们不处理. |--Exception |--RuntimeException 运行 ...

  9. orm 语法 数据库连接、建表、增删改查、回滚、单键关联 、多键关联、三表关联

    1.数据库连接, #!usr/bin/env/python # -*- coding:utf-8 -*- # from wangteng import sqlalchemy from sqlalche ...

  10. 移动端touchstart、touchmove事件的基本使用

    在pc端,我们通常使用$(window).scroll()事件来监听元素的位置,来做一些入场动效,如: $(window).scroll(function(){ var panel3Move = do ...