(floyd+DP) zoj 3027

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<cmath>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<map>
using namespace std;
map<string,int> mp;
char s1[12],s2[12];
int n,cnt,dp[2][205][205];
void floyd()
{
    for(int k=1;k<=cnt;k++)
    {
        for(int i=1;i<=cnt;i++)
        {
            for(int j=1;j<=cnt;j++)
                dp[0][i][j]=min(dp[0][i][k]+dp[0][k][j],dp[0][i][j]);
        }
    }
}
void solve()
{
    for(int k=1;k<=cnt;k++)
    {
        for(int i=1;i<=cnt;i++)
        {
            for(int j=1;j<=cnt;j++)
            {
                dp[1][i][j]=min(min(dp[1][i][k]+dp[0][k][j],dp[0][i][k]+dp[1][k][j]),dp[1][i][j]);
            }

        }
    }
}
int main()
{
    while(scanf("%s%s",s1,s2)!=EOF)
    {
        int ex,ey;
        mp.clear();
        mp[s1]=++cnt;
        if(mp[s2]==0) mp[s2]=++cnt;
        ex=mp[s1],ey=mp[s2];
        scanf("%d",&n);
        memset(dp,0x3f,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            int w;
            scanf("%s%s%d",s1,s2,&w);
            if(mp[s1]==0) mp[s1]=++cnt;
            if(mp[s2]==0) mp[s2]=++cnt;
            dp[0][mp[s1]][mp[s2]]=w;
            dp[1][mp[s1]][mp[s2]]=0;
        }
        for(int i=1;i<=cnt;i++)
            dp[0][i][i]=dp[1][i][i]=0;
        floyd();
        solve();
        printf("%d\n",dp[1][ex][ey]);
    }
    return 0;
}

  

posted @ 2015-05-18 11:25  waterfull  阅读(146)  评论(0编辑  收藏  举报