Part Acquisition
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4694 | Accepted: 1999 | 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.
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.
* 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.
* 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.
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
刚拿过来一看,蒙圈,用最短路来解吧,他没有权值,用bfs感觉很复杂,想了一下午,决定用dijkstra,权值都是1,他是单向图,突然顿悟,即使权值一样,也有最短路径一说,权值设为一,就像记点数似的,走了多少个点,每次都选最少的点,能少走就不多走。
还有个问题就是输出路径,这里用个path记录前驱,然后在递归输出,因为每次在更新的时候,就这串代码
for(j=1;j<=k;j++)
{
if(!vis[j]&&dis[j] > dis[p]+map[p][j])
{
dis[j]=dis[p]+map[p][j];
path[j]=p;
}
}
只需要改变这个点对应的前驱,就可以把他的路径的改变记录下来,如果记录后继的话,很麻烦,
ac代码:
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int n,k;
int map[1005][1005];
int vis[1005],path[1005];
int dis[1005];
void output(int t)
{
if(path[t]==-1)
return ;
else
{
output(path[t]);
printf("%d\n",t);
}
}
void dijkstra()
{
int i,j;
for(i=1;i<=k;i++)
{
dis[i]=map[1][i];
if(i!=1 && map[1][i]<INF)
path[i]=1;
}
int min,p;
vis[1]=1;
for(i=1;i<=k;i++)
{
min=INF;
for(j=1;j<=k;j++)
{
if(!vis[j]&&dis[j] < min)
{
min=dis[j];
p=j;
}
}
vis[p]=1;
for(j=1;j<=k;j++)
{
if(!vis[j]&&dis[j] > dis[p]+map[p][j])
{
dis[j]=dis[p]+map[p][j];
path[j]=p;
}
}
}
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&k))
{
for(i=0;i<=k;i++)
{
for(j=0;j<=k;j++)
{
map[i][j]=INF;
}
vis[i]=0;
path[i]=-1;//让他默认的前驱变成-1
}
int u,v;
for(i=0;i<n;i++)
{
scanf("%d%d",&u,&v);
map[u][v]=1;
}
dijkstra();
if(dis[k]<INF)
{
printf("%d\n",dis[k]+1);
printf("1\n");
output(k);
}
else
{
printf("-1\n");
}
}
return 0;
}