题目链接:

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. java基础知识-比较运算符

    演示比较运算符 == : 判断两个值是否相等 != : 判断两个数是否不相等(不能写成<>) > :判断左边值是否大于右边值 < :判断左边值是否小于右边值 >= : 判 ...

  2. cocos游戏网址

    http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framewor ...

  3. FastReport使用技巧

    使用技巧篇 1.FastReport中如果访问报表中的对象?       可以使用FindObject方法.      TfrxMemoView(frxReport1.FindObject('memo ...

  4. Linux-文件查找命令find

    find - search for files in a directory hierarchy find命令用于在目录层级中查找文件 SYNOPSIS find [-H] [-L] [-P] [-D ...

  5. EF t4模板将实体与DBContext分离

    在用EF DBFirst时,实体类是从数据库自动生成的,与DBContext放在同一个项目中.这样其他项目想引用实体,就会将数据库操作类暴露出来.所以,我们需要将实体分离. 新建项目EFAccess, ...

  6. 在.net中修改Webbrowser控件的IE版本

    根据32位.64位系统来分别修改对应的注册表路径的键值对,不需要重启程序. /// <summary> /// 修改Webbrowser控件模拟的IE版本 /// </summary ...

  7. php防sql注入过滤代码

    防止sql注入的函数,过滤掉那些非法的字符,提高sql安全性,同时也可以过滤XSS的攻击. function filter($str) { if (empty($str)) return false; ...

  8. centos7上mysql5.6版本主从复制

    做主从复制实验: 第一步:主服务器上操作 1.修改主服务器master: [root@localhost ~]# vim /etc/my.cnf server_id = 1  //[必须]服务器唯一I ...

  9. vue.js生命周期钩子函数及缓存

    在使用vue.js进行开发时,使用最多的就是created.mounted.activated. 由于有些情况下,我们需要复用某些组件,因此需要用到keep-alive. 当引入keep-alive时 ...

  10. 连接企业的人、事、物、知识--企业IM的第三类生存方式

    企业IM现状:尴尬 传统的企业IM在完成聊天.群组.文件传输.音频会话等传统IM功能后,发现自己陷入了尴尬的境地,因为功能的“同质化”,这些功能微信.QQ也有啊,其他IM厂商也有.于是IM厂商开始思考 ...