码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 2027 Travelling Fee (最短路变形)

时间:2014-09-06 18:41:53      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   io   ar   strong   for   

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"stdio.h"
#include"string.h"
#include"vector"
#include"queue"
#include"iostream"
#include"algorithm"
using namespace std;
#define N 205
const int inf=(int)1e10;
int g[N][N],dis[N];
bool mark[N];
int n,ans;
void inti()
{
    int i,j;
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
            g[i][j]=(i==j?0:inf);
    }
}
void dijkstra(int s,int t)
{
    int i,min,sum=0;
    memset(mark,0,sizeof(mark));
    mark[s]=1;
    for(i=0;i<n;i++)
        dis[i]=g[s][i];
    dis[s]=0;
    while(1)
    {
        s=0;
        min=inf;
        for(i=0;i<n;i++)
        {
            if(!mark[i]&&min>dis[i])
            {
                min=dis[i];
                s=i;
            }
        }
        if(s==0)
            break;
        sum+=min;
        mark[s]=1;
        if(s==t)
            break;
        for(i=0;i<n;i++)
        {
            if(!mark[i]&&dis[i]>dis[s]+g[s][i])
                dis[i]=g[s][i];
        }
    }
    ans=ans<sum?ans:sum;
}
int main()
{
    int i,j,k,m,a,b,d;
    char str[N][15],s1[15],s2[15];
    while(scanf("%s %s",s1,s2)!=-1)
    {
        inti();
        strcpy(str[0],s1);
        strcpy(str[1],s2);
        n=2;
        scanf("%d",&m);
        while(m--)       //输入边
        {
            scanf("%s %s %d",s1,s2,&d);
            for(i=0;i<n;i++)
                if(strcmp(str[i],s1)==0)
                    break;
            if(i==n)
                strcpy(str[n++],s1);
            a=i;
            for(i=0;i<n;i++)
                if(strcmp(str[i],s2)==0)
                    break;
            if(i==n)
                strcpy(str[n++],s2);
            b=i;
            g[a][b]=d;
        }
        ans=inf;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i!=j&&g[i][j]<inf) //枚举每条边置为零
                {
                    int t=g[i][j];
                    g[i][j]=0;
                    dijkstra(0,1);
                    g[i][j]=t;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


zoj 2027 Travelling Fee (最短路变形)

标签:des   style   blog   color   os   io   ar   strong   for   

原文地址:http://blog.csdn.net/u011721440/article/details/39102865

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!