题目链接:

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——最短路问题变型)的更多相关文章

  1. zoj 2027 Travelling Fee

    // 题意 : 一个人要去旅行 给你起点和终点 求最少花费 其中花费为经过路径的总费用减去该路径的中的最大花费段// 直接搜索 稍微加了个剪枝 主要是数据规模小#include <iostrea ...

  2. ZOJ1027 Travelling Fee(DP+SPFA)

    给一张有向无环图,边都有花费,从某点到某点走的那条路径上的那一条花费最多的边可以省掉,问从起点到终点的最少花费的多少, 往DP想的话,就可以写出这个状态dp[u][mx],表示到达u点已经省掉的花费为 ...

  3. Travelling

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU-3001 Travelling

    http://acm.hdu.edu.cn/showproblem.php?pid=3001 从任何一个点出发,去到达所有的点,但每个点只能到达2次,使用的经费最小.三进制 Travelling Ti ...

  5. hdu 3001 Travelling (TSP问题 )

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. hdu 3001 Travelling(状态压缩 三进制)

    Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. Travelling(spfa+状态压缩dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ...

  8. 【状压dp】Travelling

    [hdu3001]Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. HDU3001 Travelling

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. shell 命令 查看本机ip

    ifconfig 结果有很多,查看env0的inet,就是本机的ip地址

  2. unidbgrid单元格操作

    unidbgrid单元格操作 //GRID里回车替换TABfunction cellkeydown(sender, td, cellIndex, record, tr, rowIndex, e, eO ...

  3. Winform解决界面重绘闪烁的问题

    在窗体或用户控件中重写CreateParams protected override CreateParams CreateParams { get { CreateParams cp = base. ...

  4. Download SQL Server Management Studio (SSMS)下载地址

    Download SQL Server Management Studio (SSMS)下载地址: https://msdn.microsoft.com/en-us/library/mt238290. ...

  5. 操作Checkbox标签

    在前端开发中,少不了对Checkbox的操作. 常用的的方法有2个:.is()和.prop()方法.前者是判断 checkbox的状态,选不是未选.而后者为checkbox设置一个值,可以设置chec ...

  6. [初识]使用百度AI接口,图灵机器人实现简单语音对话

    一.准备 1.百度ai开放平台提供了优质的接口资源https://ai.baidu.com/  (基本免费) 2.在语音识别的接口中, 对中文来说, 讯飞的接口是很好的选择https://www.xf ...

  7. 关于Maven整合SSM项目中报错Invalid bound statement (not found):的问题解决

    如图:控制不报错 页面就是报500的错误 查阅了好多资料  都说是Mapper文件写的不对  我仔细找了好几遍也解决不了问题.. 解决: 坑爹的问题害我找了一上午原因,原来是需要在pom.xml文件中 ...

  8. web开发常用网络优化

    优化方法: 1.合并资源文件,减少HTTP请求 2.压缩资源文件减少请求大小 3.利用缓存机制,尽可能使用缓存减少请求 如何做前端路由 html5 api中的history能够让我们控制url跳转之后 ...

  9. IOS渗透测试第一步-基础知识统一放送

    原文: http://www.websecgeeks.com/2017/04/ios-application-pentesting-part-3.html http://www.websecgeeks ...

  10. css3的帧动画

    概述 前几天刚好看到一个用了CSS3帧动画的页面,对它非常感兴趣,就研究了一下,记录在下面,供以后开发时参考,相信对其他人也有用. PS:以后别人问我用过什么CSS3属性的时候,我也可以不用说常见的a ...