(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;
}

  

时间: 2024-07-28 17:17:03

(floyd+DP) zoj 3027的相关文章

(floyd+DP) zoj 1232

Adventure of Super Mario Time Limit: 2 Seconds      Memory Limit: 65536 KB After rescuing the beautiful princess, Super Mario needs to find a way home -- with the princess of course :-) He's very familiar with the 'Super Mario World', so he doesn't n

[dp] zoj 3769 Diablo III

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3769 Diablo III Time Limit: 2 Seconds      Memory Limit: 65536 KB Diablo III is an action role-playing video game. A few days ago, Reaper of Souls (ROS), the new expansion of Diablo I

UVA10269 Adventure of Super Mario(Floyd+DP)

UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs to find a way home -- with the princess of course :-) He's very familiar with the 'Super Mario World', so he doesn't need a map, he only needs the be

[dp] zoj 3740 Water Level

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5122 Water Level Time Limit: 2 Seconds      Memory Limit: 65536 KB Hangzhou is a beautiful city, especially the West Lake. Recently, the water level of the West Lake got lower and lower

[递推dp] zoj 3747 Attack on Titans

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5170 Attack on Titans Time Limit: 2 Seconds      Memory Limit: 65536 KB Over centuries ago, mankind faced a new enemy, the Titans. The difference of power between mankind and their newf

[dp] zoj 3682 E - Cup 3

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3682 E - Cup 3 Time Limit: 3 Seconds      Memory Limit: 65536 KB The 2012 Europe Cup was over and Spain won the Championship. The fans of Spain want to hold some activities to celebra

zoj 1232 Adventure of Super Mario (Floyd+dp)

Adventure of Super Mario Time Limit: 2 Seconds      Memory Limit: 65536 KB After rescuing the beautiful princess, Super Mario needs to find a way home -- with the princess of course :-) He's very familiar with the 'Super Mario World', so he doesn't n

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

(状态压缩DP) zoj 3471

T - Most Powerful Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3471 Appoint description:  System Crawler  (2015-04-16) Description Recently, researchers on Mars have discovered N powerful atoms