# [BZOJ1681]&&[Usaco2005 Mar]Checking an Alibi 不在场的证明

## 描述 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.

## 输出格式 OutputFormat

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.

3 2 10 4
1 2 3
3 1 10
2
2
2
2
3
3
3
2
2
2

7
1
2
3
4
8
9
10

Silver

BZOJ 1681

## 代码 Code

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x7fffffff/11.27;
int i,j,t,n,m,l,r,k,z,y,x;
inline void read(int &x)
{
x=0;char ch=getchar();
while (ch<'0' || ch>'9') ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
}
#include <queue>
struct edge
{
int to,next,val;
}e;
int head,dis;
bool used,can;
deque <int> q;
int c,f,p,cnt,ans;
inline void ins(int u,int v,int w)
{
e[++cnt].to=v;e[cnt].next=head[u];e[cnt].val=w;
head[u]=cnt;
}
inline void spfa(int s)
{
int i,j,t,u,v,w;
memset(used,false,sizeof(used));
while (!q.empty()) q.pop_front();
for (i=1;i<=f;i++) dis[i]=inf;
used[s]=true;dis[s]=0;q.push_back(s);
while (!q.empty())
{
u=q.front();q.pop_front();used[u]=false;
for (i=head[u];i;i=e[i].next)
{
v=e[i].to;w=e[i].val;
if (dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if (!used[v])
{
used[v]=true;
if (!q.empty() && dis[q.front()]>dis[v]) q.push_front(v);
else q.push_back(v);
}
}
}
}
}
int main()
{
cnt=0;memset(can,false,sizeof(can));
read(f);read(p);read(c);read(m);
for (i=1;i<=p;i++)
{
read(x);read(y);read(z);
ins(x,y,z);
ins(y,x,z);
}
spfa(1);
for (i=1;i<=c;i++)
{
read(x);
if (dis[x]<=m)
{
ans++;
can[i]=true;
}
}
printf("%d\n",ans);
for (i=1;i<=c;i++) if (can[i]) printf("%d\n",i);
return 0;
}``````