HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost
http://acm.hdu.edu.cn/showproblem.php?pid=1385
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.
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:
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.
4
0 2 3 9
2 0 1 5
3 1 0 3
9 5 3 1
0 0 0 0
1 4
-1 -1
4
0 2 3 9
2 0 1 5
3 1 0 3
9 5 3 1
1 2 3 4
1 4
-1 -1
Sample Output
From 1 to 4 :
Path: 1-->2-->3-->4
Total cost : 6
From 1 to 4 :
Path: 1-->2-->4
Total cost : 9
解题思路:将最短路保存起来,最短路相同,比较字典序,输出字典序最小的那个方案,此题难点也是按字典序输出!
具体方案见源代码!
解题代码:
// File Name: Minimum Transport Cost 1385.cpp
// Author: sheng
// Created Time: 2013年07月18日 星期四 23时36分58秒 #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std; const int max_n = ;
const int INF = 0x3fffffff;
int map[max_n][max_n], cos[max_n];
int vis[max_n], dis[max_n];
int cun[max_n];
int bg, ed, n; int out(int x, int y, int bg)
{
if (x != bg)
x = out (cun[x], x, bg);
printf("%d%s", x, x == ed ? "\n" : "-->");
return y;
} int sort(int j, int k)
{
int path_1[max_n];
int path_2[max_n];
int len1 = , len2 = ;
path_1[len1 ++] = j;
for (int i = k; ; i = cun[i])
{
path_1[len1 ++] = i;
if (i == bg)
break;
}
for (int i = j; ; i = cun[i])
{
path_2[len2 ++] = i;
if (i == bg)
break;
}
len1 --;
len2 --;
int len = len1 < len2 ? len1 : len2;
for (int i = ; i <= len; i ++)
{
if (path_1[len1 - i] < path_2[len2 - i])
return ;
if (path_1[len1 - i] > path_2[len2 - i])
return ;
}
if (len1 < len2)
return ;
return ; } int main ()
{
while (~scanf ("%d", &n), n)
{
for (int i = ; i <= n; i ++)
for (int j = ; j <= n; j ++)
scanf ("%d", &map[i][j]);
for (int i = ; i <= n; i ++)
scanf ("%d", &cos[i]);
int T = ;
while (scanf ("%d%d", &bg, &ed) && bg != - && ed != -)
{ memset(vis, , sizeof (vis));
for (int i = ; i <= n; i ++)
{
if (map[bg][i] != - && bg != i)
{
dis[i] = map[bg][i] + cos[i];
cun[i] = bg;
}
else dis[i] = INF;
}
dis[bg] = ;
vis[bg] = ;
for (int i = ; i <= n; i ++)
{
int k;
int min = INF;
for (int j = ; j <= n; j++)
{
if (!vis[j] && min > dis[j])
{
min = dis[j];
k = j;
}
}
vis[k] = ;
for (int j = ; j <= n; j ++)
if (!vis[j] && map[k][j] != -)
{
if ( dis[j] > dis[k] + map[k][j] + cos[j])
{
cun[j] = k;
dis[j] = map[k][j] + dis[k] + cos[j];
}
else if (dis[j] == dis[k] + map[k][j] + cos[j])//花费相同时,寻找字典序最小的方案
{
if (sort(j, k))//比较字典序
cun[j] = k;
}
} }
printf ("From %d to %d :\n", bg, ed);
printf ("Path: ");
out (ed, , bg);//输出路径
printf ("Total cost : %d\n\n", bg == ed ? : dis[ed] - cos[ed]);
}
}
return ;
}
HDU 1385 Minimum Transport Cost (Dijstra 最短路)的更多相关文章
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
- hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu 1385 Minimum Transport Cost (Floyd)
Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】
<题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...
- hdu 1385 Minimum Transport Cost (floyd算法)
貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...
- hdu 1385 Minimum Transport Cost
http://acm.hdu.edu.cn/showproblem.php?pid=1385 #include <cstdio> #include <cstring> #inc ...
- HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )
链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...
- HDU 1385 Minimum Transport Cost 最短路径题解
本题就是使用Floyd算法求全部路径的最短路径,并且须要保存路径,并且更进一步须要依照字典顺序输出结果. 还是有一定难度的. Floyd有一种非常巧妙的记录数据的方法,大多都是使用这种方法记录数据的. ...
- 【HDOJ】1385 Minimum Transport Cost
Floyd.注意字典序!!! #include <stdio.h> #include <string.h> #define MAXNUM 55 #define INF 0x1f ...
随机推荐
- 应聘复习基础笔记1:网络编程之TCP与UDP的优缺点,TCP三次握手、四次挥手、传输窗口控制、存在问题
重要性:必考 一.TCP与UDP的优缺点 ①TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据.TCP提供 ...
- ociuldr 支持分多个数据文件
在审计工作,将几亿条的oracle数据通过sqlserver自带工具导入到sqlserver中,速度不是特别的理想,虽然通过视图方式能提高一些速度,但是既不简洁,也不方便. 用ociuldr工具,可以 ...
- 初探oracle删除重复记录,只保留rowid最小的记录
如题,初探oracle删除重复记录,只保留rowid最小的记录(rowid可以反映数据插入到数据库中的顺序) 一.删除重复记录可以使用多种方法,如下只是介绍了两种方法(exist和in两种). 1.首 ...
- Android的快速开发框架afinal
afinal 是一个android的 orm 和 ioc 框架.而且封装了android中的httpClient,使其更加简单易用. afinal是android应用开发的终极框架. FinalDB使 ...
- 在 App 扩展和主 App 间共享数据
tags: iOS 8,Swift,App Groups 随着 iOS 8 的发布,苹果为广大开发者很多新的 API,其中最突出显著的就非 App Extension 莫属了.这为开发者们又带来了很多 ...
- swift someObject == nil 如何实现
以前的编程经验告诉我们判断一个对象是否为空或者是否实例化可以这样 if(someObject == nil){ //do something }else{ } 但是在Swift中这样是不行的,然后我在 ...
- Redis 客户端配置及示例
一.redis自定义配置节点 <configSections> <section name ="RedisConfig" type="Amy.Toolk ...
- UITableView swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- Java Day 11
异常 Throwable 子类 Error.Exception Error - 不可以处理 Exception - 针对性处理 原理 自定义异常 异常类的抛出throws 先检查语法错误,后检查逻辑 ...
- Android -- Home按键
游戏中常常需要监听android HOME键,当HOME键下压时,往往需要做一些状态保存,音效停止等操作,那么如何做,才能监听到HOME键呢?我们知道HOME是系统键,app中无法通过onKey这些函 ...