zoukankan      html  css  js  c++  java
  • [Usaco2005][BZOJ1674] Part Acquisition|dijkstra|priority_queue

    1674: [Usaco2005]Part Acquisition

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 308  Solved: 143
    [Submit][Status][Discuss]

    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


    OUTPUT DETAILS:

    The cows possess 4 objects in total: first they trade object 1 for
    object 3, then object 3 for object 2, then object 2 for object 5.

    HINT

     

    Source

     
    抄了个堆优化dijkstra的模板……
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #define inf 1000000000
    #define pa pair<int,int>
    using namespace std;
    int n,m,cnt,dis[1005],head[1005];
    bool vis[1005];
    int next[50005],list[50005],key[50005];
    inline int read()
    {
        int a=0,f=1; char c=getchar();
        while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
        while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
        return a*f;
    }
    inline void insert(int u,int v,int w)
    {
        next[++cnt]=head[u];
        head[u]=cnt;
        list[cnt]=v;
        key[cnt]=w;
    }
    inline void dijkstra()
    {
        priority_queue<pa,vector<pa>,greater<pa> > q;
        for (int i=1;i<=n;i++) dis[i]=inf,vis[i]=0;
        dis[1]=0;
        q.push(make_pair(0,1));
        while (!q.empty())
        {
            int now=q.top().second; q.pop();
            if (vis[now]) continue; vis[now]=1;
            for (int i=head[now];i;i=next[i])
            {
                if (dis[list[i]]>dis[now]+key[i])
                {
                    dis[list[i]]=dis[now]+key[i];
                    q.push(make_pair(dis[list[i]],list[i]));
                }
            }
        }
    }
    int main()
    {
        m=read(); n=read();
        for (int i=1;i<=m;i++) 
        {
            int u=read(),v=read();
            insert(u,v,1);
        }
        dijkstra();
        if (dis[n]==inf) puts("-1"); else printf("%d",dis[n]+1);
        return 0;
    }
  • 相关阅读:
    C#
    Excel 中大量图片如何快速导出? 转载自:http://www.zhihu.com/question/20800948
    IE的F12开发人员工具不显示 转载自:http://blog.csdn.net/longyulu/article/details/8749705
    firefox ie 比较 relative path
    fiddler save files
    selenium3加载浏览器
    Linux安装PHP
    客户端级别的渲染分析工具 dynaTrace
    前端性能分析:分析百度和sogou
    Linux vi的基本操作
  • 原文地址:https://www.cnblogs.com/ws-fqk/p/4766508.html
Copyright © 2011-2022 走看看