zoukankan      html  css  js  c++  java
  • (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
    ",dp[1][ex][ey]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    C程序之修改Windows的控制台大小
    C程序之修改Windows的控制台颜色(转载)
    VS2010/MFC编程(对话框:模态对话框及其弹出过程)
    C/C++常用头文件及函数汇总
    vs2010点调试,显示系统找不到指定的文件
    C++之类和对象的使用(三)
    C++之类和对象的使用(二)
    idea激活方式
    Java之dom4j的简单解析和生成xml的应用
    Java之POI的excel导入导出
  • 原文地址:https://www.cnblogs.com/water-full/p/4511483.html
Copyright © 2011-2022 走看看