# bzoj 1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明（BFS）

## 1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 389   Solved: 250
[ Submit][ Status][ Discuss]

## 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 F
1 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 numb
er 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.

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

BFS，答案要按顺序输出，有重边和自环

#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct
{
int y;
int t;
}Line;
Line now;
queue<int> q;
vector<Line> G;
vector<int> C;
int bet, ans, vis;
int main(void)
{
int n, m, p, x, y, t, T, i, cnt, j;
scanf("%d%d%d%d", &n, &m, &p, &T);
for(i=1;i<=m;i++)
{
scanf("%d%d%d", &x, &y, &t);
if(t>T)  continue;
now.y = y, now.t = t;
G[x].push_back(now);
now.y = x;
G[y].push_back(now);
}
memset(bet, 62, sizeof(bet));
bet = 0;
for(i=1;i<=p;i++)
{
scanf("%d", &x);
C[x].push_back(i);
}
memset(vis, 0, sizeof(vis));
q.push(1);
vis = 1;
while(q.empty()==0)
{
x = q.front();
q.pop();
vis[x] = 0;
for(i=0;i<G[x].size();i++)
{
y = G[x][i].y;
t = G[x][i].t;
if(bet[x]+t<bet[y] && bet[x]+t<=T)
{
bet[y] = bet[x]+t;
if(vis[y]==0)
{
q.push(y);
vis[y] = 1;
}
}
}
}
cnt = 0;
for(i=1;i<=n;i++)
{
if(bet[i]<=T)
{
for(j=0;j<C[i].size();j++)
ans[++cnt] = C[i][j];
}
}
sort(ans+1, ans+cnt+1);
printf("%d\n", cnt);
for(i=1;i<=cnt;i++)
printf("%d\n", ans[i]);
return 0;
}

01-16 29                                                                          10-19 25
09-14 32
05-09 313
06-15 954
08-06 37
09-13 1577
10-24 672
04-13 414
10-21 841
10-20 785
10-31 936
04-08 1118
10-31 1022
09-29 657
04-13 76
09-01 895
05-17 745
07-19 525
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客