zoj 2027 Travelling Fee (最短路变形)

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;
}
时间: 2024-10-11 05:04:00

zoj 2027 Travelling Fee (最短路变形)的相关文章

ZOJ 2027 Travelling Fee

枚举+最短路 题意是说出发地 和 目的地 之间有一条边是免费的.问你最小费用. 误区:求出最短路-路径中的最大边.(有些其他边免费之后,可能最短路就变了) 正确思路:枚举每条边,将其费用设为0.然后求最短路.找费用最小. 这是无向图,至于地名可以用map映射. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #inclu

zoj 1655 Transport Goods (最短路变形)

Transport Goods Time Limit: 2 Seconds      Memory Limit: 65536 KB The HERO country is attacked by other country. The intruder is attacking the capital so other cities must send supports to the capital. There are some roads between the cities and the

URAL 1934 Black Spot --- 简单最短路变形

边权为1,在维护最短路的同时维护p值最小,我直接存的(1-p),即不遇见的概率,要使得这个值最大. #include <iostream> #include <cstdlib> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #includ

UESTC 915 方老师的分身II --最短路变形

即求从起点到终点至少走K条路的最短路径. 用两个变量来维护一个点的dis,u和e,u为当前点的编号,e为已经走过多少条边,w[u][e]表示到当前点,走过e条边的最短路径长度,因为是至少K条边,所以大于K条边的当做K条边来处理就好了.求最短路的三个算法都可以做,我这里用的是SPFA,比较简洁. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #incl

zoj1655 最短路变形

题意:HERO过的首都需要货物,需要从其他的城市吧货物送到首都,每条道路都会需要消耗一定比例的货物,问最多能送多少货物到首都. 思路:如果每个点的比例是1,到达首都的比例就是经过的路径的(1-消耗比)的乘积,反正是无向的,所以可以反过来推,首都的货物比是1,而到达每座 城市的货物就是所经过的路径(1-消耗比)的乘积,则由此可见,我们可以求首都到任意城市的最大比值:最后把每个点的最大比值乘以每个点的货物加起来 即是结果. #include<stdio.h> #include<string.

HN0I2000最优乘车 (最短路变形)

HN0I2000最优乘车 (最短路变形) [试题]为了简化城市公共汽车收费系统,某城市决定对大部分的公共汽车都采用一票制,但由于某些公共汽车所经过的停车站太多和路途太长,就采用两票或多票制.经过这种票制改革后,人们坐公共汽车从一个站到另一个站时,就不得不选择一个好的乘车方案,以使他们的乘车费用最低. 为了方便于求出最佳的乘车方案,我们假设: l  采用一票制的公共汽车,无论从哪个站上车到那个站下车,乘该公共汽车的费用为1(费用单位). l  采用多票制的公共汽车,将设立某些站为关键站:那么,如果

ZOJ - 3794 Greedy Driver 最短路

首先正向跑一遍1为起点的最短路,注意松弛过程如果走到加油站则dis=0,并且路上任意时刻dis都不能大于C,判断dis[n]是否<=C就能判断无解情况了. 然后反向建图再跑一次N为起点的最短路,这样可以求到每个点到n点的最短路. 对于每一个可以交易的城市,C-dis1[i]-dis2[i]就是多出来可以卖掉的油. #include<iostream> #include<cstdlib> #include<cstring> #include<cmath>

POJ 1797 Heavy Transportation (最短路变形)

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 20364   Accepted: 5401 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

zoj 1655 单源最短路 改为比例+最长路

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=655 没有理解清题意就硬套模板,所以WA了好几次, 解析看我的另一篇http://blog.csdn.net/u011026968/article/details/35579035 贴代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #de