Part Acquisition
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4080 | Accepted: 1742 | 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
题意;母牛想用草换k星奶机;输出路径;bellman只是单纯的找dis的最小值,会陷入2 3,3 2循环
spfa:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=1010;
const int MAXM=50010;
int vis[MAXN],dis[MAXN],pre[MAXN];
int head[MAXM];
queue<int>S;
int n,k,edgnum;
struct Edge{
int from,to,next;
}edg[MAXM];
void initial(){
mem(head,-1);
edgnum=0;
mem(pre,0);
}
void add(int u,int v){
Edge E={u,v,head[u]};
edg[edgnum]=E;
head[u]=edgnum++;
}
void print(int x){
if(x==0)return;
print(pre[x]);
printf("%d\n",x);
}
void spfa(int sx){
while(!S.empty())S.pop();
mem(vis,0);mem(dis,INF);
S.push(sx);vis[sx]=1;dis[sx]=1;
while(!S.empty()){
int u=S.front();
vis[u]=0;
S.pop();
for(int i=head[u];i!=-1;i=edg[i].next){
int v=edg[i].to;
if(dis[v]>dis[u]+1){
dis[v]=dis[u]+1;
if(!vis[v]){
pre[v]=u;
vis[v]=1;
S.push(v);
}
}
}
}
if(dis[k]==INF){
puts("-1");
return;
}
printf("%d\n",dis[k]);
print(k);
return;
}
int main(){
while(~scanf("%d%d",&n,&k)){
initial();
int i,j,u,v;
F(i,n){
scanf("%d%d",&u,&v);
add(u,v);
}
spfa(1);
}
return 0;
}
bellman死循环:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=50010;
struct Node{
int u,v;
}dt[MAXN<<1];
int dis[MAXN],pre[MAXN];
int edgnum;
int n,k;
void add(int u,int v){
dt[edgnum].u=u;
dt[edgnum++].v=v;
}
void print(int x){
if(pre[x]==0)return;
print(pre[x]);
printf("%d\n",x);
}
int Bellman(int u){
mem(dis,INF);dis[u]=0;
for(int i=1;i<=k;i++){
int u,v;
for(int j=0;j<edgnum;j++){
// dis[v]=min(dis[v],dis[u]+1);
if(dis[v]>dis[u]+1){
dis[v]=dis[u]+1;
pre[v]=u;
}
}
}
if(dis[k]==INF)return -1;
print(k);
return dis[k];
}
int main(){
while(~scanf("%d%d",&n,&k)){
int i,j;
edgnum=0;
mem(pre,0);
F(i,n){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
printf("%d\n",Bellman(1));
}
return 0;
}
dijkscra;不能输出路径。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=1010;
const int MAXM=50010;
int vis[MAXN],dis[MAXN],pre[MAXN];
int mp[MAXN][MAXN];
int n,k,edgnum;
void print(int x){
if(x==0)return;
print(pre[x]);
printf("%d\n",x);
}
void dijkscra(int sx){
mem(dis,INF);mem(vis,0);
dis[sx]=1;//vis[sx]=1;
while(true){
int t=-1,i;
F(i,n){
if(!vis[i]&&(t==-1||dis[i]<dis[t]))t=i;
}
if(t==-1)break;
vis[t]=1;
F(i,n)dis[i]=min(dis[i],dis[t]+mp[t][i]);
}
if(dis[k]==INF){
puts("-1");return;
}
printf("%d\n",dis[k]);
//print(k);
}
int main(){
while(~scanf("%d%d",&n,&k)){
mem(mp,INF);mem(pre,0);
int i,j,u,v;
F(i,n){
scanf("%d%d",&u,&v);
mp[u][v]=1;
}
dijkscra(1);
}
return 0;
}