Travelling Fee(Dijlstra——最短路问题变型)
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2027
题目:
Samball is going to travel in the coming vacation. Now it's time to make a plan. After choosing the destination city, the next step is to determine the travel route. As this poor guy has just experienced a tragic lost of money, he really has limited amount of money to spend. He wants to find the most costless route. Samball has just learned that the travel company will carry out a discount strategy during the vacation: the most expensive flight connecting two cities along the route will be free. This is really a big news.
Now given the source and destination cities, and the costs of all the flights, you are to calculate the minimum cost. It is assumed that the flights Samball selects will not have any cycles and the destination is reachable from the source.
Input
The input contains several test cases, each begins with a line containing names
  of the source city and the destination city. The next line contains an integer
  m (<=100), the number of flights, and then m lines follow, each contains
  names of the source city and the destination city of the flight and the corresponding
  cost. City names are composed of not more than 10 uppercase letters. Costs are
  integers between 0 to 10000 inclusively.
Process to the end of file.
Output
For each test case, output the minimum cost in a single line.
Sample Input
HANGZHOU BEIJING
  2
  HANGZHOU SHANGHAI 100
  SHANGHAI BEIJING 200
Sample Output
100
/*
问题 题目本身不难,关键是理解题意,求起始城市到目标城市的最少花费,也即最短路,很容易陷入的误区是先求一条最短路,再找出该条最短路上
最大花费并减去,要明白,这样找的最短路没错,但是减去最大花费之后是不能保证整体花费最小的
所以解题思路是
处理数据成邻接矩阵存储数据;由于免去一条花费最高的边,索性枚举每一条边使其为0,计算m次最短路,得出最小的那一个即可
*/
#include<stdio.h>
#include<string.h>
struct Edge{
char from[],to[];
int cost;
}edge[];
struct City{
char name[];
int num;
}city[];
const int inf=;
int map[][],citynum;
char source[],destin[];
int dis[],vis[],path[];
int startnum,endnum; int ret_citynum(char temp[]);
int Dijkstra();
int main()
{
int m,i,j;
while(scanf("%s%s",source,destin) != EOF)
{
scanf("%d",&m);
for(i=;i<=m;i++){
scanf("%s%s%d",edge[i].from,edge[i].to,&edge[i].cost);
}
citynum=;
memset(map,-,sizeof(map));
for(i=;i<=m;i++){
map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=edge[i].cost;
}
for(i=;i<=citynum;i++){
for(j=;j<=citynum;j++){
if(map[i][j]==-)
map[i][j]=inf;
}
}
/*for(i=1;i<=citynum;i++){
printf("%s编号为%d\n",city[i].name,city[i].num);
}
for(i=1;i<=citynum;i++){
for(j=1;j<=citynum;j++){
printf("%8d",map[i][j]);
}
printf("\n");
}*/
startnum=ret_citynum(source);
endnum=ret_citynum(destin); int ans=inf,temp;
for(i=;i<=m;i++){
map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=;
temp=Dijkstra();
if(temp < ans)
ans = temp;
map[ret_citynum(edge[i].from)][ret_citynum(edge[i].to)]=edge[i].cost;
}
printf("%d\n",ans);
}
}
int Dijkstra()
{
int i,j;
memset(vis,,sizeof(vis));
for(i=;i<=citynum;i++){
if(i != startnum)
dis[i]=inf;
else
dis[startnum]=;
}
for(i=;i<=citynum;i++){
int x,min=inf;
for(j=;j<=citynum;j++){
if(!vis[j] && dis[j] <= min)
min=dis[x=j];
}
vis[x]=;
for(j=;j<=citynum;j++){
if(dis[j] > dis[x] + map[x][j])
dis[j] = dis[x] + map[x][j];
}
}
return dis[endnum];
}
int ret_citynum(char temp[])
{
int i;
for(i=;i<=citynum;i++){
if(strcmp(temp,city[i].name)==)
return i;
} citynum++;
strcpy(city[citynum].name,temp);
city[citynum].num=citynum;
return citynum;
}
Travelling Fee(Dijlstra——最短路问题变型)的更多相关文章
- zoj  2027 Travelling Fee
		// 题意 : 一个人要去旅行 给你起点和终点 求最少花费 其中花费为经过路径的总费用减去该路径的中的最大花费段// 直接搜索 稍微加了个剪枝 主要是数据规模小#include <iostrea ... 
- ZOJ1027 Travelling Fee(DP+SPFA)
		给一张有向无环图,边都有花费,从某点到某点走的那条路径上的那一条花费最多的边可以省掉,问从起点到终点的最少花费的多少, 往DP想的话,就可以写出这个状态dp[u][mx],表示到达u点已经省掉的花费为 ... 
- Travelling
		Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ... 
- HDU-3001                        Travelling
		http://acm.hdu.edu.cn/showproblem.php?pid=3001 从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小.三进制 Travelling Ti ... 
- hdu 3001 Travelling (TSP问题 )
		Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ... 
- hdu 3001 Travelling(状态压缩 三进制)
		Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ... 
- Travelling(spfa+状态压缩dp)
		题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ... 
- 【状压dp】Travelling
		[hdu3001]Travelling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ... 
- HDU3001 Travelling
		Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ... 
随机推荐
- 冲刺博客NO.3
			今天做了什么:参考网上的一些登录界面,发现了Android studio自带loginActivity.做了基础登录界面 不停地上网搜,各种不会. 在短信验证功能上通过在Mob.com的集成文档和官 ... 
- 笑话库存加网址http://www.jokeji.cn/list18_11.htm
			19.富二代王晓伟成绩很差,老爸想给他找个家教老爸:“儿子,想找什么样的家教啊?”儿子:“要漂亮的,女的,衣服不能太保守,花样要多!”老爸:“儿子,你TM指的是岛国的苍老师吗?”@呦呦ta爹 20.哥 ... 
- 笔记1:jmeter性能测试使用示例(原文:http://blog.csdn.net/zhongweijian/article/details/7619319)
			jmeter是一个简单开源的纯java的性能测试工具.今天学习了jmeter使用了下jmeter,使用起来非常简单. 如果我们要对163的首页性能进行简单测试,我们可以按照以下步骤进行. 1.在测试计 ... 
- c#中快速排序的学习
			最近看了一句话,说的是在现实生活中,会写字的人不见得会写出好文章,学习编程语言就像是学会了写字,学会了编程语言并不一定能写出好程序. 我觉得就是很有道理,以前读书的时候,基本学完了C#中的语法知识,算 ... 
- wpf 的依赖属性只能在loaded 事件之后才能取到
			wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的 InitializeComponent(); 之后取不到 wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的 ... 
- 一分钟学会git
			首先 克隆 源码地址 git clone git://github.com/jquery/jquery.git 更新 git pull查看状态 git status暂存所有(注意 . 表示全部暂存) ... 
- 3、JUC--ConcurrentHashMap 锁分段机制
			ConcurrentHashMap  Java 5.0 在 java.util.concurrent 包中提供了多种并发容器类来改进同步容器的性能.  ConcurrentHashMap 同步容器 ... 
- webApp在各大Android市场上的发布
			本来打算每个月都写上一篇博客的,可是计划永远赶不上变化,不过这其中也有自己的懒惰,果然过年让整个人懈怠了不少.年后一直在赶项目以致于到今天才动手写这篇文章. 这一篇主要写点在公司的要求下发布的webA ... 
- postgresql-pg_prewarm数据预加载。
			pg_prewarm数据预加载. http://francs3.blog.163.com/blog/static/405767272014419114519709/ https://www.kan ... 
- 13-01 java StringBuffer类,StringBuilder类
			StringBuffer类的构造方法 package cn.itcast_01; /* * 线程安全(多线程讲解) * 安全 -- 同步 -- 数据是安全的 * 不安全 -- 不同步 -- 效率高一些 ... 
