题目链接:

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. Android之TextView灵活使用

    Android之TextView灵活使用 在项目中有无遇到过这样一种程况,例如文字"王明今年10岁了", 但是数字10是从网络返回的数据, 而你又想把这个文字写在xml中, 过往我 ...

  2. 利用C#迭代器的一个杨辉三角示例

    身边有个朋友在跟着廖雪峰的教程学习python,途中遇到了"在Python中使用迭代器打印杨辉三角"的问题,我在帮忙解决的同时顺手写了个简单的C#版本以供补充. internal ...

  3. docker - 从安装到部署一个web应用(go、java)

    一:安装docker 1.https://docs.docker.com/engine/installation/binaries/ 下载docker最新版二进制tar.gz linux下: wget ...

  4. Django:常见的orm操作

    ArticlePost模型对应的表如下: 1.查询两个日期之间2019.04.20到2019.04.25之间的文章 import datetime from.models import Article ...

  5. 【NumberValidators】工商营业执照号码和统一社会信用代码验证

    从本质上讲,工商营业执照号码和统一社会信用代码是两套完全不一样的编码规则,识别结果也仅有行政区划部分为两者共有,但因为这两种编码同时存在的原因,所以如果需要在系统中唯一标志一家企业时,还是可以通过工商 ...

  6. Microsoft.Office.Interop.Excel.ApplicationClass can not embedded 的问题

    用c#进行开发时,要做一个excel导入功能,期间使用到Microsoft.Office.Interop.Excel程序集,在用vs2008开发的时候没有报错,将这个程序集引用到vs2010的时候,便 ...

  7. ASP.Net MVC OA项目笔记<六>

    1.1.1 开始写业务,先写业务的实现再写业务的接口 业务类中也是有写增删改查公用的方法 引用Model,IDAL,DALFactory BLL添加两个类 UserInfoService,BaseSe ...

  8. 企业项目开发--cookie(2)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 2.1.3.CookieUtil:(cookie的基本操作:增删查,注意没有改)  1 package co ...

  9. libffi-dev : 依赖: libffi6 (= 3.2.1-4) 但是 3.2.1-4kord 正要被安装

    原文链接:https://blog.csdn.net/u013992330/article/details/85135629 中标麒麟安装python库:sshtunnel 的时候提示缺少ffi.h文 ...

  10. 如何查看 Ubuntu下已安装包版本号

    原文链接:https://www.cnblogs.com/the-tops/p/8350662.html 一个软件工具叫做apt-show-versions,通过apt-get安装: $sudo ap ...