zoj2027Travelling Fee(Spfa+枚举)

来源:互联网 发布:淘宝淘抢购在哪里报名 编辑:程序博客网 时间:2024/05/02 08:17

题目链接:

huangjing

题意:
给了起始和终点城市,然后给了若干对城市和距离,然后从起点到终点最小的费用,但是有一个新优惠,那就是费用最大的两个城市之间可以免费。
思路:
最开始以为求了最短路然后减去最大的费用即可。但是想了一组样例就知道是错的。
比如1--->2---->3 然后有直接1----->3,那么如果按刚才的思路,那么最小费用就是2
      2     5                  8
所以思路是错的。。然后在cp的帮助下得知,既然不知道哪条是最大的费用路,那么我们就对每条路进行枚举,假设这条是最长路。。然后做n次spfa,取最小值。。。。

 题目:

Travelling Fee

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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*/





































0 0