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. [Qt]用QItemDelegate的来修改QStandardItem字体颜色

    1.重写ItemDelegate的Item方法 这里我使用的QListView来显示Log日志,将写好的代理在初始化中就可以直接赋值上. m_LogModel = new QStandardItemM ...

  2. 进度条ProgressDialog

    1.效果图 public void click(View view) { final ProgressDialog pdDialog = new ProgressDialog(this); //设置标 ...

  3. Shell判断字符串包含关系的几种方法

    现在每次分析网站日志的时候都需要判断百度蜘蛛是不是真实的蜘蛛,nslookup之后需要判断结果中是否包含“baidu”字符串 以下给出一些shell中判断字符串包含的方法,来源程序员问答网站 stac ...

  4. .NET 面向对象基础

    封装                                                                                                   ...

  5. linux6的yum源

    [base]name=CentOS-$releasever-Basebaseurl=http://centos.ustc.edu.cn/centos/6/os/x86_64/gpgcheck=1gpg ...

  6. vagrant安装及使用方法

    http://www.chenjie.info/1757 http://blog.csdn.net/zsl10/article/category/6324870   --以下转自MaxWellDuva ...

  7. CSC321 神经网络语言模型 RNN-LSTM

    主要两个方面 Probabilistic modeling 概率建模,神经网络模型尝试去预测一个概率分布 Cross-entropy作为误差函数使得我们可以对于观测到的数据 给予较高的概率值 同时可以 ...

  8. 【Java EE 学习 44】【Hibernate学习第一天】【Hibernate对单表的CRUD操作】

    一.Hibernate简介 1.hibernate是对jdbc的二次开发 2.jdbc没有缓存机制,但是hibernate有. 3.hibernate的有点和缺点 (1)优点:有缓存,而且是二级缓存: ...

  9. 使用DotNetBar制作漂亮的WinFrom界面,自定义AgileEAS.NET SOA平台WinClient主界面

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  10. 【leetcode】Search Insert Position

    题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...