zoj2027Travelling Fee(Spfa+枚举)
来源:互联网 发布:淘宝淘抢购在哪里报名 编辑:程序博客网 时间:2024/05/02 08:17
题目链接:
题意:
给了起始和终点城市,然后给了若干对城市和距离,然后从起点到终点最小的费用,但是有一个新优惠,那就是费用最大的两个城市之间可以免费。
思路:
最开始以为求了最短路然后减去最大的费用即可。但是想了一组样例就知道是错的。
比如1--->2---->3 然后有直接1----->3,那么如果按刚才的思路,那么最小费用就是2
2 5 8
所以思路是错的。。然后在cp的帮助下得知,既然不知道哪条是最大的费用路,那么我们就对每条路进行枚举,假设这条是最长路。。然后做n次spfa,取最小值。。。。
题目:
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
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<vector>#include<cmath>#include<string>#include<queue>#define eps 1e-9#define ll long long#define INF 0x3f3f3f3fusing namespace std;const int maxn=100+10;const int maxm=maxn*2+10;map<string,bool>Judge;map<string,int>mp;struct Edge{ int to,val,next;}edge[maxn],temp[maxn];int st,en,n,m,cnt,cal;int dis[maxm],head[maxm];bool vis[maxm];char start[maxm],End[maxm];queue<int>Q;int Spfa(int k){ while(!Q.empty()) Q.pop(); memset(vis,false,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); for(int i=1;i<=cnt;i++) temp[i]=edge[i]; temp[k].val=0; dis[1]=0; vis[1]=true; Q.push(1); while(!Q.empty()) { int Gery=Q.front(); Q.pop(); for(int i=head[Gery];i!=-1;i=temp[i].next) { int small_milan=temp[i].to; int val=temp[i].val; if(dis[small_milan]>dis[Gery]+val) { dis[small_milan]=dis[Gery]+val; if(!vis[small_milan]) { vis[small_milan]=true; Q.push(small_milan); } } } } return dis[2];}void add_edge(int x,int y,int val){ edge[++cnt].to=y; edge[cnt].val=val; edge[cnt].next=head[x]; head[x]=cnt;}void read_Graph(){ int w; memset(head,-1,sizeof(head)); scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%s%s%d",start,End,&w); if(!Judge[start]) { Judge[start]=true; mp[start]=++cal; } if(!Judge[End]) { Judge[End]=true; mp[End]=++cal; } add_edge(mp[start],mp[End],w); add_edge(mp[End],mp[start],w); }}int main(){ while(~scanf("%s%s",start,End)) { Judge.clear(); mp.clear(); cal=cnt=0; Judge[start]=true; Judge[End]=true; mp[start]=++cal; mp[End]=++cal; read_Graph(); int ans=INF; for(int i=1;i<=cnt;i++) ans=min(ans,Spfa(i)); printf("%d\n",ans); } return 0;}/*a b3a b 2b c 5a c 8*/
- zoj2027Travelling Fee(Spfa+枚举)
- Fee
- hdu1595(枚举+最短路,Dijkstra/SPFA)
- poj 1062 spfa+枚举
- hdu 5137 枚举+spfa
- POJ3594-枚举+spfa
- poj 昂贵的聘礼(spfa)(枚举)
- POJ 1135 Domino Effect (spfa + 枚举)- from lanshui_Yang
- 1062 昂贵的聘礼 (spfa + 等级枚举) - from lanshui_Yang
- HDU 2363 Cycling(并查集 SPFA 暴力枚举)
- hdu 2363 Cycling (最短路spfa + 暴力枚举)
- ZOJ 2027 Travelling Fee(最短路)
- zoj 2027 Travelling Fee ( 两种解法 1.SPFA 2.floyd()+dp )
- spfa+枚举 hdu 3768 Shopping
- 哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)I 旅行【枚举+spfa】
- [U]3.2.6 Sweet Butter 枚举+SPFA
- HDU 3768 Shopping spfa+dfs枚举
- BZOJ-1880 Elaxia的路线 SPFA+枚举
- loadrunner测试论坛
- 二叉树总结_legend
- Java数组操作的10大方法
- UITableView基础
- java编程思想(第四版)第二章练习题-3
- zoj2027Travelling Fee(Spfa+枚举)
- iOS开发之音乐播放----监听耳机拔插
- Java实现AOP —— 模拟动态代理
- MFC 工具栏上加控件
- Unable to resolve superclass of Lcom/example/view/MainActivity
- 试用例设计方法 等价类划分法
- 怎样搭建SSH框架
- synchronized 语句块中到底锁住了什么对象 ?
- 2440开发板,210开发板,imx6开发板和4412开发板选择哪个更好呢?