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.
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.
* 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.
* 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:
Any cow except cow 5 could have done it. Cow 5 would take 9 seconds to get to the barn.
Fields/distances like this:
6 4------5 | | 2| | | | 7-----1 |5 9 | | 1| | | | 2------3OUTPUT DETAILS:
Any cow except cow 5 could have done it. Cow 5 would take 9 seconds to get to the barn.
分析:
很简单的单源最短路问题,只要以第一个点为起点,算出到另外所有点的最短路径,最后跟限制的时间比较就好了.第一次用Dijkstra不是很熟悉...INF开始设为1001导致我数次WA...
code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdlib>
#define MAX 500
#define INF 0x7f7f7f7f
using namespace std;
int f,p,c,m;
int mp[MAX+5][MAX+5];
int dis[MAX+5];
bool vis[MAX+5];
int ans[100+5];
void Init()
{
int s,e,len;
for(int i=0;i<MAX+5;i++)
for(int j=0;j<MAX+5;j++)
mp[i][j]=INF;
memset(vis,false,sizeof(vis));
memset(ans,0,sizeof(ans));
scanf("%d %d %d %d",&f,&p,&c,&m);
for(int i=0;i<p;i++)
{
scanf("%d %d %d",&s,&e,&len);
if(mp[s][e]>len)
mp[s][e]=mp[e][s]=len;
}
}
void Dijkstra()
{
dis[1]=0;
for(int i=2;i<=f;i++)
dis[i]=INF;
for(int i=1;i<f;i++)
{
int tmp=0,min=INF;
for(int j=1;j<=f;j++)
if(!vis[j]&&dis[j]<min)
{
min=dis[j];
tmp=j;
}
vis[tmp]=true;
for(int j=1;j<=f;j++)
if(mp[tmp][j]!=INF&&dis[j]>dis[tmp]+mp[tmp][j]) dis[j]=dis[tmp]+mp[tmp][j];
}
}
int main()
{
//freopen("input.txt","r",stdin);
int num=0;
Init();
Dijkstra();
for(int i=1;i<=c;i++)
{
int cow;
scanf("%d",&cow);
if(dis[cow]<=m)
ans[num++]=i;
}
printf("%d\n",num);
for(int i=0;i<num;i++) printf("%d\n",ans[i]);
return 0;
}