Flight HDU - 3499 (hierarchical shortest)

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 (ie 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?
graduate, Lele intend to travel over and over again. Target cities had been set, fly to. For high school graduates, airlines have a preferential policies, you can choose a half-price line, 100 change 50,99 becomes 49. You help him design a program, so that the total cost to a minimum.
INPUT
There are NO More Within last 10 Test Cases. Subsequent Test Cases are Separated by Line A blank.
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 "XY 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 "SE" representing the start and end city. X, Y, S, E are all strings consisting of at most 10 alphanumeric characters.
how the test group the first line of two integers m and n, m denotes n cities and routes the next row m, each row represents a path from x to y, s takes two strings membered last row, represents the hair Lele the starting point and destination cities.
The Output
One Line for each Test Case The Least Money Shua Shua have have to Pay. Impossible for the If apos IT HIM to Finish The TRIP, Just Output -1. Minimum cost output, if the output does not reach -1
the Sample the Input
. 4. 4
Harbin Beijing 500
Harbin 1000 shanghai
Beijing Chengdu 600
Chengdu 400 shanghai
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.
样例一中: Harbin->Beijing->Chengdu ,第二条路线使用半价

Meaning of the questions:
questions that Italy faces a Chinese
thinking:
is low version https://www.cnblogs.com/qieqiemin/p/11298508.html this question

This is a routine problem, based on the shortest path to open a dis-dimensional maintenance and use several preferential information can transfer the same as dp.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const ll inf = 1e18;
/*** TEMPLATE CODE * * STARTS HERE ***/
struct node
{
    int to;
    int kk;
    ll val;
    node(){}
    node(int tt,int ww,ll vv)
    {
        kk=ww;
        to=tt;
        val=vv;
    }
    bool operator < (const node & b) const
    {
        return val>b.val;
    }
};
std::vector<node> e[maxn];
ll dis[maxn][15];
unordered_map<string,int> info;
void addedge(int a,int b,ll v)
{
    e[a].push_back(node(b,0,v));
}
void init(int n)
{
    for(int i=1;i<=n;++i)
    {
        for(int j=0;j<=2;j++)
            dis[i][j]=inf;
    }
}
priority_queue<node> heap;
int n;
int m,k=1;
void dijkstra(int strat)
{
    init(n);
    dis[strat][0]=0ll;
    heap.push(node(strat,0,0ll));
    node temp;
    while(!heap.empty())
    {
        temp=heap.top();
        heap.pop();
        int now=temp.to;
        ll val=temp.val;
        int kk=temp.kk;
        if(kk>k)
        {
            continue;
        }
        if(val>dis[now][kk])
            continue;
        for(auto x:e[now])
        {
            if(dis[x.to][kk]>val+x.val)
            {
                dis[x.to][kk]=val+x.val;
                heap.push(node(x.to,kk,dis[x.to][temp.kk]));
            }
            if(kk<k&&dis[x.to][temp.kk+1]>val+x.val/2)
            {
                dis[x.to][kk+1]=val+x.val/2;
                heap.push(node(x.to,kk+1,dis[x.to][temp.kk+1]));
            }

        }
    }
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    while(cin>>n>>m)
    {
        info.clear();
        repd(i,1,n)
        {
            e[i].clear();
        }
        int u,v;ll c;
        string str1,str2;
        int cnt=0;
        while(m--)
        {
            cin>>str1>>str2>>c;
            if(info.count(str1)==0)
            {
                info[str1]=++cnt;
            }
            if(info.count(str2)==0)
            {
                info[str2]=++cnt;
            }
            u=info[str1];
            v=info[str2];
            addedge(u,v,c);
        }
        int s,t;
        cin>>str1>>str2;
        if(info.count(str1)==0)
        {
            info[str1]=++cnt;
        }
        if(info.count(str2)==0)
        {
            info[str2]=++cnt;
        }
        s=info[str1];
        t=info[str2];
        dijkstra(s);
        ll ans=inf;
        repd(i,0,1)
        {
            ans=min(ans,dis[t][i]);
        }
        if(ans==inf)
            ans=-1;
        cout<<ans<<endl;
    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

Guess you like

Origin www.cnblogs.com/qieqiemin/p/11298770.html