HDU - 3499 Flight 双向SPFA+枚举中间边

Flight

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?

InputThere 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. 
OutputOne 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

Hint

In the first sample, Shua Shua should use the card on the flight from
 Beijing to Chengdu, making the route Harbin->Beijing->Chengdu have the
 least total cost 800. In the second sample, there‘s no way for him to get to
Chengdu from Harbin, so -1 is needed. 

题意:ShuaShua要从一个城市到另一个城市,给出每两城市之间的花费(有向),ShuaShua可以有一次半价的机会,求最小花费。思路:最短路径问题。开始想到求出最短路后选择其中最大的边/2,但其实错误,很容易举出反例:路径1:1 1 100 路径2:30 30 30 开始时102 90选择花费少的路径2,减半价后52 75路径1花费反而相对更少。因此我们可以换一种思路,以起点和终点为单源分别求出到各点的最短路,然后枚举每一条边作为中间边,dis[u]+w(u,v)/2+diss[v]的最小值为解。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<deque>
#include<map>
#include<string>
#define MAX 100005
#define MAXX 500005
#define INF 10000000000000000
using namespace std;

struct Node{
    int v,w;
}node;
vector <Node> edge[MAX],redge[MAX];
map<string,int> mp;
long long dis[MAX],diss[MAX],b[MAX],u[MAXX],v[MAXX],w[MAXX];
char s1[MAX],s2[MAX];
int n;

void spfa1(int k)
{
    int i;
    deque<int> q;
    for(i=1;i<=n;i++){
        dis[i]=INF;
    }
    memset(b,0,sizeof(b));
    b[k]=1;
    dis[k]=0;
    q.push_back(k);
    while(q.size()){
        int u=q.front();
        for(i=0;i<edge[u].size();i++){
            int v=edge[u][i].v;
            long long w=edge[u][i].w;
            if(dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                if(b[v]==0){
                    b[v]=1;
                    if(dis[v]>dis[u]) q.push_back(v);
                    else q.push_front(v);
                }
            }
        }
        b[u]=0;
        q.pop_front();
    }
}

void spfa2(int k)
{
    int i;
    deque<int> q;
    for(i=1;i<=n;i++){
        diss[i]=INF;
    }
    memset(b,0,sizeof(b));
    b[k]=1;
    diss[k]=0;
    q.push_back(k);
    while(q.size()){
        int u=q.front();
        for(i=0;i<redge[u].size();i++){
            int v=redge[u][i].v;
            long long w=redge[u][i].w;
            if(diss[v]>diss[u]+w){
                diss[v]=diss[u]+w;
                if(b[v]==0){
                    b[v]=1;
                    if(diss[v]>diss[u]) q.push_back(v);
                    else q.push_front(v);
                }
            }
        }
        b[u]=0;
        q.pop_front();
    }
}

int main()
{
    int m,bg,ed,t,i,j;
    while(~scanf("%d%d",&n,&m)){
        t=0;
        for(i=1;i<=n;i++){
            edge[i].clear();
            redge[i].clear();
            mp.clear();
        }
        memset(u,0,sizeof(u));
        memset(v,0,sizeof(v));
        memset(w,0,sizeof(w));
        for(i=1;i<=m;i++){
            scanf(" %s%s%lld",s1,s2,&w[i]);
            if(!mp[s1]) mp[s1]=++t;
            if(!mp[s2]) mp[s2]=++t;
            u[i]=mp[s1];
            v[i]=mp[s2];
            node.v=mp[s2];
            node.w=w[i];
            edge[mp[s1]].push_back(node);
            node.v=mp[s1];
            redge[mp[s2]].push_back(node);
        }
        scanf(" %s%s",s1,s2);
        if(!mp[s1]) mp[s1]=++t;
        if(!mp[s2]) mp[s2]=++t;
        bg=mp[s1],ed=mp[s2];
        spfa1(bg);
        spfa2(ed);
        long long min=INF;
        for(i=1;i<=m;i++){
            if(dis[u[i]]==INF||dis[v[i]]==INF) continue;
            if(dis[u[i]]+diss[v[i]]+w[i]/2<min) min=dis[u[i]]+diss[v[i]]+w[i]/2;
        }
        if(min==INF) printf("-1\n");
        else printf("%lld\n",min);
    }
    return 0;
}
时间: 2024-10-29 19:07:45

HDU - 3499 Flight 双向SPFA+枚举中间边的相关文章

hdu 3499 Flight dijkstra 变形

Problem Description 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 hel

HDU 3499 Flight spfa+dp

Flight Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Recently, Shua Shua had a big

hdu 4430 Yukari&#39;s Birthday 枚举+二分

注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3262    Accepted Submission(s): 695 Problem Description Today is Yukari's n-th birt

Zoj 3088 Easter Holidays SPFA+枚举

其实就是枚举最高点和起点,然后以最高点为源点在两张图上分别做spfa.一遍最短路,一遍最长路. 暴露出来的问题:思维不够清晰,代码能力还不够 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include &

hdu 2489 Minimal Ratio Tree 枚举+最小生成树

点的总数很小,直接枚举就好. #include <stdio.h> #include <string.h> #define N 20 #define inf 1000000 int mk[N],n,k,ans[N]; double low[N],val[N]; double map[N][N],MIN; double prim() { int i,j; double sum=0; double tot=0; for(i=1;i<=n;i++) low[i]=inf; int

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+

hdu 1217 Arbitrage Floyd||SPFA

转载请注明出处:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1

hdu 4309 Seikimatsu Occult Tonneru 枚举+最大流

http://blog.csdn.net/julyana_lin/article/details/8070949 题意: n个点,每个点有初始的值 ,三种 通道,1.隧道:可以用来躲避,有固定的容量,也可以用来传递.2.普通的道路,可以无限的通过.3.桥(最多有12座):不花费的话能通过一人,修之后可以无限通过.问最少花费最大可以隐藏人数. 解: 网络流 + 枚举 官方题解: 先不考虑可以修复的桥的性质,则可以将模型简化为n个点的人通过有通过人数上限的有向边,到达一些有人数上限的特殊的边(隧道)

zoj2027Travelling Fee(Spfa+枚举)

题目链接: huangjing 题意: 给了起始和终点城市,然后给了若干对城市和距离,然后从起点到终点最小的费用,但是有一个新优惠,那就是费用最大的两个城市之间可以免费. 思路: 最开始以为求了最短路然后减去最大的费用即可.但是想了一组样例就知道是错的. 比如1--->2---->3 然后有直接1----->3,那么如果按刚才的思路,那么最小费用就是2 2     5                  8 所以思路是错的..然后在cp的帮助下得知,既然不知道哪条是最大的费用路,那么我们就