zoukankan      html  css  js  c++  java
  • POJ 2457 Part Acquisition

    Part Acquisition
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2909   Accepted: 1262   Special Judge

    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). 

    * Lines 2..T+1: The ordered list of the objects that the cows possess in the sequence of trades.

    Sample Input

    6 5
    1 3
    3 2
    2 3
    3 1
    2 5
    5 4

    Sample Output

    4
    1
    3
    2
    5

    Hint

    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.

    Source

     
     
    题意:cows 想用自己手上的商品(1)通过多次交换得到想要的商品(k),给出两种商品的交换关系,求出至少有多少种商品经过交换,输出交换的顺序。

    思路:最短路。相当于求从1商品到k商品的最短路径,中间再记录一下结点信息。我用的是边表实现dij算法
     
      1 #include<iostream>
      2 #include<queue>
      3 #include<cstdio>
      4 #include<cstring>
      5 
      6 using namespace std;
      7 
      8 const int maxn=50010;
      9 const int INF=0x3f3f3f3f;
     10 
     11 struct PP{
     12     int id;
     13     int len;
     14     bool operator < (const PP &a) const{
     15         return a.len<len;
     16     }
     17 }dis[maxn];
     18 
     19 int vis[maxn],head[maxn],pre[maxn],ans[maxn];
     20 int id;
     21 
     22 struct node{
     23     int v,w;
     24     int next;
     25 }e[100010];
     26 
     27 void addedge(int u,int v,int w){    //有向边 
     28     e[id].v=v;
     29     e[id].w=w;
     30     e[id].next=head[u];
     31     head[u]=id;
     32     id++;
     33 }
     34 
     35 int n,src,des;
     36 
     37 void Dijkstra(){
     38     priority_queue<PP> Q;
     39     while(!Q.empty())
     40         Q.pop();
     41     memset(vis,0,sizeof(vis));
     42     for(int i=1;i<=n;i++){
     43         dis[i].id=i;
     44         dis[i].len=INF;
     45     }
     46     dis[src].len=0;
     47     dis[src].id=src;
     48     Q.push(dis[src]);
     49     PP tmp;
     50     while(!Q.empty()){
     51         tmp=Q.top();
     52         Q.pop();
     53         int now=tmp.id;
     54         if(vis[now])
     55             continue;
     56         vis[now]=1;
     57         for(int i=head[now];i!=-1;i=e[i].next){
     58             int len=e[i].w;
     59             int to=e[i].v;
     60             if(dis[to].len>dis[now].len+len){
     61                 dis[to].len=dis[now].len+len;
     62                 pre[to]=now;
     63                 Q.push(dis[to]);
     64             }
     65         }
     66     }
     67 }
     68 
     69 int main(){
     70 
     71     //freopen("input.txt","r",stdin);
     72 
     73     src=1;
     74     while(~scanf("%d%d",&n,&des)){
     75         id=0;
     76         memset(head,-1,sizeof(head));
     77         int u,v;
     78         for(int i=1;i<=n;i++){
     79             scanf("%d%d",&u,&v);
     80             addedge(u,v,1);
     81         }
     82         Dijkstra();
     83         if(dis[des].len!=INF){
     84             printf("%d\n",dis[des].len+1);
     85             int cnt=0;
     86             ans[cnt++]=des;
     87             int cur=des;
     88             while(pre[cur]!=1){
     89                 ans[cnt++]=pre[cur];
     90                 cur=pre[cur];
     91             }
     92             ans[cnt]=1;
     93             for(int i=cnt;i>=0;i--){
     94                 printf("%d",ans[i]);
     95                 if(i!=0)
     96                     printf("\n");
     97             }
     98         }else
     99             printf("-1\n");
    100     }
    101     return 0;
    102 }
  • 相关阅读:
    真情感动看上海新闻娱乐频道“百家心”
    Treeview Navigation Web Part for SharePoint with SmartPart v1.0
    轻松玩转Typed DataSet, Part III
    微软Avalon和WinForms的发展蓝图
    Proxy Pattern using C#
    System.Threading.Timer类的TimerCallback 委托
    SmartPart v1.0 for SharePoint [Free Web Part]
    分布式应用架构中的数据传输对象(DTO)
    定制SharePoint Portal Server 2003站点的向导
    Bridge Pattern using C#
  • 原文地址:https://www.cnblogs.com/jackge/p/3019827.html
Copyright © 2011-2022 走看看