zoukankan      html  css  js  c++  java
  • Bzoj 1674: [Usaco2005]Part Acquisition Dij / Spfa

    1674: [Usaco2005]Part Acquisition

    Description

    The cows have been sent on a mission through space to acquire a new milking machine for their barn. They are flying through a cluster of stars containing N (1 <= N <= 50,000) planets, each with a trading post. The cows have determined which of K (1 <= K <= 1,000) types of objects (numbered 1..K) each planet in the cluster desires, and which products they have to trade. No planet has developed currency, so they work under the barter system: all trades consist of each party trading exactly one object (presumably of different types). The cows start from Earth with a canister of high quality hay (item 1), and they desire a new milking machine (item K). Help them find the best way to make a series of trades at the planets in the cluster to get item K. If this task is impossible, output -1.

    Input

    * Line 1: Two space-separated integers, N and K. * Lines 2..N+1: Line i+1 contains two space-separated integers, a_i and b_i respectively, that are planet i's trading trading products. The planet will give item b_i in order to receive item a_i.

    Output

    * Line 1: One more than the minimum number of trades to get the milking machine which is item K (or -1 if the cows cannot obtain item K).

    Sample Input

    6 5 //6个星球,希望得到5,开始时你手中有1号货物.
    1 3 //1号星球,希望得到1号货物,将给你3号货物
    3 2
    2 3
    3 1
    2 5
    5 4

    Sample Output

    4

    思路:

     最短路裸题, 把输入看成点和边, 边权均为1 跑一遍Dij即可, 我是反着跑的, 答案一样, 注意反向加边 另外用的配对堆以期实现nlogn的复杂度

    #include <cstdio>
    #include <ext/pb_ds/priority_queue.hpp>
    #include <iostream>
    #include <cstring>
    #define ll long long
    /*#define  __attribute__((optimize("-O2")))*/
    #define mp(a, b) make_pair(a, b)
    using namespace std;
    using namespace __gnu_pbds;
    #define heap __gnu_pbds::priority_queue<pair<ll, int> >
    const ll INF = 1e15;
    const int N = 1000001, M = 1000001;
    struct node {
        int to, val, next;
    }e[M];
    int n, m, head[N], cnt;
    ll f[N];
    heap q;
     inline char nc() {
        static char buf[100000], *p1, *p2;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin), p1==p2)?EOF:*p1++;
    }
     inline int read() {
        char c=nc();int x=0;
        while(!isdigit(c))c=nc();
        while(isdigit(c)){x=(x<<3)+(x<<1)+(c^'0');c=nc();}
        return x;
    }
     inline void add(int x, int y, int z) {
        e[++cnt].to = y;
        e[cnt].next = head[x];
        head[x] = cnt;
        e[cnt].val = z;
    }
    heap::point_iterator id[N];
     int main() {
        /*#ifndef ONLINE_JUDGE
            freopen("3040.in", "r", stdin);
        #endif*/
        int n=read(), m=read(); //m -= read(); read();read();read();read();read();
        register int i;
        int x, y;
        for(i=1;i<=n;i++) {
            x=read(), y=read();
            add(y, x, 1);
        }
        for(i=1;i<m;i++) f[i] = INF;
        id[m] = q.push(mp(0, m));
        while(!q.empty()) {
            int u = q.top().second;q.pop();
            for(i=head[u];i;i=e[i].next) {
                if(f[e[i].to]>f[u]+e[i].val) {
                    f[e[i].to] = f[u] + e[i].val;
                    if(id[e[i].to]!=0) q.modify(id[e[i].to], mp(-f[e[i].to], e[i].to));
                    else id[e[i].to] = q.push(mp(-f[e[i].to], e[i].to));
                }
            }
        }
        printf("%lld", (f[1]+1)>1e9?-1:f[1]+1);
    }
    
  • 相关阅读:
    2020-3-23学习地图
    HashMap<K,V>类
    2020-3-21学习地图
    模板模式
    2020-3-20学习地图
    字符串常量池String Constant Pool
    2020-3-19学习地图
    2020-3-18学习地图
    MySQL游标
    2020-3-16学习地图
  • 原文地址:https://www.cnblogs.com/Tobichi/p/9247632.html
Copyright © 2011-2022 走看看