Part Acquisition
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3900 | Accepted: 1689 | 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.
题目意思:牛儿想要得到牛奶机,有N个转换途径可供其选择 即价值a的物品换取价值b的物品,问牛能否用价值1的干草换到价值K的牛奶机? 若能输出转化过程中牛拥有的最少物品数(感觉就是最短路加一)并输出转换路径,反之输出-1。
思路:对每个转换途径 建权值为1的边,然后求1到K的最短路,只不过转化过程的物品数需要算上起始的干草。路径输出我用pre[]数组记录前一物品,从终点开始压栈,直到1结束,然后输出栈元素直至栈空。
dijkstra 和 Floyd 已经用很多次了,这次就用spfa写下:32ms
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define MAX 50000+10
#define INF 100000
using namespace std;
struct Edge
{
int to, val, next;
}edge[MAX];
int n, k;
int head[MAX];//记录下一节点指针
int dist[MAX], pre[MAX];
bool vis[MAX];
int top;
void init()
{
top = 0;
memset(head, -1, sizeof(head));
memset(pre, -1, sizeof(pre));
}
void addedge(int u, int v, int w)
{
Edge E = {v, w, head[u]};
edge[top] = E;
head[u] = top++;
}
void getMap()
{
int a, b;
for(int i = 0; i < n; i++)
{
scanf("%d%d", &a, &b);
addedge(a, b, 1);
}
}
void findpath()
{
stack<int> path;
int now = k;//从终点开始
while(1)//存储路径
{
path.push(now);//进栈
if(now == 1)
break;
now = pre[now];//上一个位置
}
while(!path.empty())//栈为空
{
now = path.top();
path.pop();
printf("%d\n", now);
}
}
void SPFA()
{
for(int i = 1; i <= n; i++)
dist[i] = i==1 ? 1 : INF; //注意建图起点到起点为1
memset(vis, false, sizeof(vis));//初始化
queue<int> Q;
Q.push(1);
vis[1] = true;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(dist[E.to] > dist[u] + E.val)
{
dist[E.to] = dist[u] + E.val;
pre[E.to] = u;
if(!vis[E.to])
{
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
if(dist[k] != INF)
{
printf("%d\n", dist[k]);
findpath();//输出路径
}
else
printf("-1\n");
}
int main()
{
while(scanf("%d%d", &n, &k) != EOF)
{
init();//初始化
getMap();//建图
SPFA();//求最短路
}
return 0;
}