【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 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

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.

Author
Edelweiss

Source
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT

題意:有一個n個結點,m條邊的有向圖,可以對一條邊進行邊權減半的操作。問起點到終點的最短路

思路(分層圖):

對於圖中邊,可以做出k次選擇(此題k=1)來改變邊權。符合分層圖的套路。構建一張平行層,這一層就相當於是原來圖的複製品。然後原來的圖(層)像新層移動的時候,如x -> y’,此邊權即是邊x->y的一半。按照這個思路,每次讀入的時候多建立一條當前層像新層過渡的邊,以及新層這兩點之間的邊。然後用這些點跑個Dijkstra即可。
可能會想,直接最短路把最長邊減半不就行了嗎。當然不行,WA點就在於,減半操作的加入,可能現在的“最短路”加進了一條新的邊。如圖:
在這裏插入圖片描述 原本最短路12+12=24,新最短路25/2=12。

最後,注意inf設置成比int大的值!

AC代碼:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxn = 1e6+5;
const ll inf=1e18;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') ch = getchar();while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
const int V = 5e5, E=3e6;
ll d[V],cost[E];
ll n,m,a,b;
ll head[V],pnt[E],nxt[E],e=0;
ll vis[V];
map<string,ll> Map;
ll s,to;

void addedge(ll u,ll v,ll c)
{
    pnt[e]=v;       //當前以u爲頂點,c爲邊長的,到v的一條邊
    cost[e]=c;      //存入當前邊權值
    nxt[e]=head[u];     //下一個其實是前一個
    head[u]=e++;        //當前邊編號
}

ll Dijkstra()
{
    priority_queue<PII, vector<PII>, greater<PII> > q;
    d[s] = 0;
    q.push(mp(0LL,s));
    while(!q.empty())
    {
        ll x = q.top().se; q.pop();
        if(vis[x]) continue;
        vis[x] = 1;
        for(int i=head[x];i!=-1;i = nxt[i])
        {
            ll v = pnt[i];
            if(d[v]>d[x]+cost[i]&&!vis[v])
            {
                d[v] = d[x] + cost[i];
                q.push(mp(d[v], v));
            }
        }
    }
    return min(d[to],d[to+n])==inf?-1:min(d[to],d[to+n]);
}


int main()
{
   // FAST;
    while(~scanf("%lld%lld",&n,&m))
    {
        Map.clear();
        mem(vis,0);mem(head,-1);mem(cost,0); e = 0;
        rep(i,1,n<<2) d[i] = inf;
        ll idx = 1;
        rep(i,1,m)
        {
            ll x, y,z;
            string s1,s2;
            char s3[15],s4[15];
            scanf("%s",s3);scanf("%s",s4);
            s1 = s3 , s2 = s4;
            z = read();
            Map[s1] = x = Map[s1]?Map[s1]:idx++;
            Map[s2] = y = Map[s2]?Map[s2]:idx++;
            addedge(x,y,z);
            addedge(x,y+n,z/2);
            addedge(x+n,y+n,z);
        }
        string s1,s2; cin>>s1>>s2;
        s = Map[s1];
        to = Map[s2];
        if(Map[s1]&&Map[s2])
        {

            cout<< Dijkstra()<<'\n';
        }
        else cout<<-1<<'\n';
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章