您的位置:首页 > 其它

POJ 2394 Checking an Alibi【SPFA】

2016-06-07 11:16 429 查看
Checking an Alibi
Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 6768

 

Accepted: 2468

Description

A crime has been comitted: a load of grain has been taken from the barn by one of FJ's cows. FJ is trying to determine which of his C (1 <= C <= 100) cows is the culprit. Fortunately, a passing satellite took an image of his farm M (1 <= M <= 70000) seconds
before the crime took place, giving the location of all of the cows. He wants to know which cows had time to get to the barn to steal the grain. 

Farmer John's farm comprises F (1 <= F <= 500) fields numbered 1..F and connected by P (1 <= P <= 1,000) bidirectional paths whose traversal time is in the range 1..70000 seconds (cows walk very slowly). Field 1 contains the barn. It takes no time to travel
within a field (switch paths). 

Given the layout of Farmer John's farm and the location of each cow when the satellite flew over, determine set of cows who could be guilty. 

NOTE: Do not declare a variable named exactly 'time'. This will reference the system call and never give you the results you really want.

Input

* Line 1: Four space-separated integers: F, P, C, and M 

* Lines 2..P+1: Three space-separated integers describing a path: F1,F2, and T. The path connects F1 and F2 and requires T seconds to traverse. 

* Lines P+2..P+C+1: One integer per line, the location of a cow. The first line gives the field number of cow 1, the second of cow 2, etc.

Output

* Line 1: A single integer N, the number of cows that could be guilty of the crime. 

* Lines 2..N+1: A single cow number on each line that is one of the cows that could be guilty of the crime. The list must be in ascending order.

Sample Input

7 6 5 8

1 4 2

1 2 1

2 3 6

3 5 5

5 4 6

1 7 9

1

4

5

3

7

Sample Output

4

1

2

3

4

Hint

INPUT DETAILS: 

Fields/distances like this: 

          6

      4------5

      |      |

     2|      |

      |      |

7-----1      |5

   9  |      |

     1|      |

      |      |

      2------3

OUTPUT DETAILS: 

Any cow except cow 5 could have done it. Cow 5 would take 9 seconds to get to the barn.

Source

USACO 2005 March Silver

 

题目大意:有F个节点,表示农场,有P条无向边,有C头牛,有距离M作为限制条件,接下来P行,表示每条边及其权值,接下来C行,第i行表示,第i头牛在多少号节点上。

现在这C头牛,都想到1号农场去偷吃东西,然而如果这头牛到1号农场的距离大于m,就可以判定这头牛偷不到吃的,问有多少头牛可以偷到吃的,然后按照升序输出牛的编号。

思路:很明显的单源最短路问题,直接跑一遍SPFA就行,然后判断一下牛能否偷到吃的即可。

AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[100000];
struct EdgeNode
{
int to;
int w;
int next;
}e[100000];
int ans[100000];
int dis[100000];
int vis[100000];
int pos[100000];
int cont,n,m,c,p;
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void SPFA(int ss)
{
for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
queue<int >s;
s.push(ss);
dis[ss]=0;
vis[ss]=1;
while(!s.empty())
{
int u=s.front();
s.pop();vis[u]=0;
for(int k=head[u];k!=-1;k=e[k].next)
{
int v=e[k].to;
int w=e[k].w;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(vis[v]==0)
{
s.push(v);
vis[v]=1;
}
}
}
}
int tot=0;
for(int i=1;i<=c;i++)
{
if(dis[pos[i]]<=p)
{
ans[tot++]=i;
}
}
printf("%d\n",tot);
for(int i=0;i<tot;i++)
{
printf("%d\n",ans[i]);
}
}
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&c,&p))
{
cont=0;
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
for(int i=1;i<=c;i++)
{
scanf("%d",&pos[i]);
}
SPFA(1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: