欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Flight HDU - 3499 双向最短路径(spfa)

程序员文章站 2022-06-03 18:32:39
...

问题:

Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to some other city to avoid meeting her. He will travel only by air and he can go to any city if there exists a flight and it can help him reduce the total cost to the destination. There's a problem here: Shua Shua has a special credit card which can reduce half the price of a ticket ( i.e. 100 becomes 50, 99 becomes 49. The original and reduced price are both integers. ). But he can only use it once. He has no idea which flight he should choose to use the card to make the total cost least. Can you help him?

Input

There are no more than 10 test cases. Subsequent test cases are separated by a blank line. 
The first line of each test case contains two integers N and M ( 2 <= N <= 100,000 

0 <= M <= 500,000 ), representing the number of cities and flights. Each of the following M lines contains "X Y D" representing a flight from city X to city Y with ticket price D ( 1 <= D <= 100,000 ). Notice that not all of the cities will appear in the list! The last line contains "S E" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters. 

Output

One line for each test case the least money Shua Shua have to pay. If it's impossible for him to finish the trip, just output -1.

Sample Input

4 4
Harbin Beijing 500
Harbin Shanghai 1000
Beijing Chengdu 600
Shanghai Chengdu 400
Harbin Chengdu

4 0
Harbin Chengdu

Sample Output

800
-1

题意:从一个点到另一个点的最小花费,其中有一个边可以是半价。

思路:最先想到的思路是,求出最短路径,在最短路径中选个最长边除以2就可以了。这是不对的。原因如下图:

Flight HDU - 3499 双向最短路径(spfa)

正确思路:例如求a到b的最小花费,求出每个点到a的最小花费dis1,在求出每个点到b的最小花费dis2,然后遍历每一条边,让每一条边都减少一半走一遍即可。代码如下:

代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define PI acos(-1.0)
#define ll long long
#define eps 1e-8
#define N 110000
#define M 510000
map<string,int>mp;
ll dis[N],dis1[N],dis2[N];
int t,x,y,k,n,m;
int a[M],b[M],c[M],book[N];
int first[M],net[M],to[M],w[M];
char h[N],s[N];
void init()
{
    mem(first,-1),mem(net,0);
    mem(book,0),k=1;
}
void add(int a,int b,int c)
{
    net[k]=first[a];
    to[k]=b;
    w[k]=c;
    first[a]=k++;
}
void spfa(int d,ll dis[])
{
    queue<int>q;
    book[d]=1;
    dis[d]=0;
    q.push(d);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        book[u]=0;
        for(int i=first[u];i!=-1;i=net[i])
        {
            int tt=to[i];
            if(dis[tt]>dis[u]+w[i])
            {
                dis[tt]=dis[u]+w[i];
                if(book[tt]==0)
                {
                    book[tt]=1;
                    q.push(tt);
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        mp.clear();t=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%s%s%d",h,s,&c[i]);
            if(mp[h]==0)mp[h]=++t;
            if(mp[s]==0)mp[s]=++t;
            a[i]=mp[h],b[i]=mp[s];
        }
        scanf("%s%s",h,s);
        if(mp[h]==0)mp[h]=++t;
        if(mp[s]==0)mp[s]=++t;
        x=mp[h],y=mp[s];
        init();
        mem(dis1,inf),mem(dis2,inf);
        for(int i=1;i<=m;i++)
            add(a[i],b[i],c[i]);
        spfa(x,dis1);
        init();
        for(int i=1;i<=m;i++)
            add(b[i],a[i],c[i]);
        spfa(y,dis2);
        ll minn=inf;
        for(int i=1;i<=m;i++)
            minn=min(minn,dis1[a[i]]+dis2[b[i]]+c[i]/2);
        if(minn==inf)printf("-1\n");
         else printf("%lld\n",minn);
    }
}

 

相关标签: 最短路