Cattle-off practice match 47 D DongDong plane (hierarchical shortest)

Links: https://ac.nowcoder.com/acm/contest/904/D
Source: Cattle-off network

DongDong flying
time limits: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 524288K, other languages 1048576K
64bit IO the Format:% LLD
subject description
is willing to Time passes slowly, I remember one of his frown smile --DongDong

DongDong home Samoyed go abroad to study, DongDong very much like him, decided to fly to visit his vacation, DongDong want to discount the rational use of the aircraft in hand so hard not to eat soil. Given n cities, m strip aircraft airline, half-price opportunity k times, 1 DongDong home, n is Samoyed home, each side has a one-way ticket prices start point and end point (to ensure that all price is greater than 0), she can k times using a 50% discount, seeking a minimum cost from 1 to n. (If not from 1 to n, the output of -1)

Input Description:
The first line of three integers, n, m, k

Next m lines each, u, v, w, indicating the presence of u to v edge consideration of w (w are all guaranteed even)

n <= 10000, m <= 50000, k <= 10,0 <= w <= 1000000 (w is an even number), the data may have multiple edges and loopback
output Description:
The first line of output minimum cost
Example 1
Input
Copy
3 2. 5
. 1 2 2
2 100. 3
. 1 100. 3
. 3 1010 2
. 1. 3 1010
output
copy
50

Ideas:
a hierarchical topic shortest path can be defined two-dimensional array dis [i] [j] represents 1 to i-node, with the j-th half price, how much distance is the shortest?

Easy to know if there is an edge point u v, then dis [v] [j] may be formed dis [u] [j] and dis [u] [j-1] transfer over.

Maintenance dis [i] [j] to the process of dijkstra.

Theoretical complexity of O (nk log nk)

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#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 = 10010;
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];
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<=14;j++)
            dis[i][j]=inf;
    }
}
priority_queue<node> heap;
int n;
int m,k;
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;
    cin>>n>>m>>k;
    int u,v;ll c;
    while(m--)
    {
        cin>>u>>v>>c;
        addedge(u,v,c);
    }
    dijkstra(1);
    ll ans=inf;
    repd(i,0,k)
    {
        ans=min(ans,dis[n][i]);
        // printf("%lld ",dis[n][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/11298508.html