POJ part acquisition

来源:互联网 发布:csv导入oracle数据库 编辑:程序博客网 时间:2024/05/22 12:14

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.

 

寻找最短路径,输出路径

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

#define inf 0x3f3f3f3f

int n,k;
int dis[50001],w[1001][1001],pre[50002];
bool vis[50001];
int path[1001],ans[1001];

void dijkstra(int x)
{
    int i,j,Min,kk=0,next;
    for(i=1;i<=k;i++)
    {
        dis[i]=w[x][i];
        if(i!=x&&dis[i]<inf)
            path[i]=x;
    }
    vis[x]=1;
    dis[x]=0;
    for(i=1;i<k;i++)
    {
        Min=inf;
        for(j=1;j<=k;j++)
        {
            if(!vis[j]&&Min>dis[j])
            {
                Min=dis[j];
                next=j;
            }
        }
        vis[next]=1;
        for(j=1;j<=k;j++)
        {
            if(!vis[j]&&dis[next]+w[next][j]<dis[j]&&w[next][j]!=inf)
            {
                dis[j]=dis[next]+w[next][j];
                path[j]=next;//j下一个要放的点,next上一次循环中已经确定要放的点
                //cout<<j<<"  "<<path[j]<<endl;
            }
        }
    }
}

void print()
{
    int i;
    memset(ans,0,sizeof(ans));
    int cnt=0;
    if(dis[k]==inf)
        cout<<"-1"<<endl;
    else
    {
        ans[cnt++]=k;
        int temp=path[k];//下标是当前点,数组值为前一个节点
        while(temp)
        {
            //cout<<temp<<endl<<endl;
            ans[cnt++]=temp;
            temp=path[temp];
            //cout<<temp<<endl;
        }
        cout<<cnt<<endl;
        for(i=cnt-1;i>=0;i--)
            cout<<ans[i]<<endl;
    }
}

int main()
{
    int i,j,a,b;
    scanf("%d%d",&n,&k);
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    //memset(w,0x3f3f3f3f,sizeof(w));
    for(i=1;i<=k;i++)
        for(j=1;j<=k;j++)
            w[i][j]=inf;
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&a,&b);
        w[a][b]=1;
    }
    dijkstra(1);
    //cout<<dis[k]+1<<endl;
    print();
    return 0;
}

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 身份证和驾驶证在外地丢了怎么办 我有摩e照学c1照怎么办 科三网上预约超过了次数限制怎么办 驾照罚款没交过了周期怎么办 驾照考了科科目一想换个驾校怎么办 韩国货物被机场海关扣了怎么办 车管所查不到居住证信息怎么办 高中毕业两年了想考大学怎么办 我买的二手货车营运证是假的怎么办 移民到欧洲国家想去日本怎么办签证 签证要写工作单位如果没有怎么办 办护照时的身份证过期了怎么办 有摩托车驾照想考小车驾照怎么办 分管副局长能直接安排工作吗怎么办 我在北京打工老婆没地方住怎么办 我的车扣了32分怎么办 济南万科地产投诉电话不管用怎么办 买手机被商家欺骗买到合约机怎么办 向消协投诉有用吗?我该怎么办? 我住南开区想办公租房不知怎么办 租房提前退房房东不退押金怎么办 体检时候眼睛有一只是弱视怎么办? b本被扣分9分了怎么办 工作调动后在新单位退休医保怎么办 社保卡和医保卡丢了怎么办 医保卡挂失后又找到了怎么办 医保卡丢失忘了卡号怎么办? 医保卡丢了怎么办又记不住卡号 住院发票丢了医保不给报销怎么办 住院期间被医院丢失了医保卡怎么办 大学时的医保卡毕业后丢了怎么办 用身份证注册的移动卡丢了怎么办 用别人身份证办的卡丢了怎么办 济南医保卡挂失后又找到了怎么办 单位没有给办理医保卡的老人怎么办 单位办的医保卡丢了怎么办 北京退休人员医保卡丢了怎么办 普通发票联丢了医保给报销怎么办 手机买好高铁票身份证丢了怎么办 买了高铁票身份证丢了怎么办 小米手机手电简打开不亮了怎么办