题目
Checking an Alibi
Time Limit: 1000MS | | Memory Limit: 65536K |
Total Submissions: 5891 | | Accepted: 2149 |
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
解题思路
题意是说有n个点,m条边,并且k个点上有牛,粮仓为1号点,让你求在p时间内有多少头牛可以到达粮仓,并且按照升序输出牛的编号。
简单的最短路问题,直接dijkstra。
源程序
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 1007
#define inf 0x3f3f3f
using namespace std;
int n,m;
int dis[M],vis[M],g[M][M],s[M],z[M];
// 初始化图g,对角线上元素为0,其余元素为inf
void init()
{
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
if(i!=j)
g[i][j]=inf;
else g[i][j]=0;
}
// dijkstra算法,单源最短路径
void dijkstra(int s)
{
int pos,minn;
for(int i=0;i<=n;i++) // 初始化各点与源点的距离dis[i]
dis[i]=g[s][i];
memset(vis,0,sizeof(vis)); // 初始化最短路径的点集vis
dis[s]=0;
vis[s]=1; // 将源点放入最短路径的点集中
for(int i=1;i<n;i++) // 寻找从源点开始的最短路径
{
pos=-1; // 记录剩余点中距离源点最近的点
minn=inf; // 记录最短路径的长度
for(int j=1;j<=n;j++) // 在剩余点中寻找与源点最近的点
if(!vis[j]&&minn>dis[j])
{
minn=dis[j];
pos=j;
}
vis[pos]=1; // 将最近的点加入最短路径的点集中
for(int j=1;j<=n;j++)
// 剩余点通过点pos到达源点,是否比通过vis中其它点到达距离更短
if(!vis[j]&&dis[j]>g[pos][j]+minn)
dis[j]=g[pos][j]+minn; // 若有,更新dis[j]
}
}
int main()
{
int k,p;
// n:点数
// m:边数
// k:牛数
// p:时间
while(scanf("%d%d%d%d",&n,&m,&k,&p)!=EOF)
{
init();
int a,b,c;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(g[a][b]>c)
g[a][b]=g[b][a]=c; // 初始化边的权值
}
dijkstra(1); // 寻找粮仓(1)到各点的最短路径
b=0;
for(int i=1;i<=k;i++)
{
scanf("%d",&z[i]); // 初始化牛所在的位置
if(dis[z[i]]<=p)
b++;
}
printf("%d\n",b); // 输出能够到达粮仓的牛数
for(int i=1;i<=k;i++)
if(dis[z[i]]<=p)
printf("%d\n",i); // 按照升序输出牛的编号
}
return 0;
}