PKU 2394---Checking an Alibi

Checking an Alibi
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3854 Accepted: 1384

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个farm,分别为f1,f2,...,fp.其中,f1是谷仓!有C只牛在这些farm上想去谷仓偷吃谷子。但是刚有有个摄像头拍下了他们吃到谷子之前M秒时的位置。并且已知这些farm之间有P条路径,例如fi与fj之间有路径连通,并且知道走这条路径的时间time(可能存在fi于fj之间有多条路径的情况)。现在告诉你C头牛M秒之前的位置,叫你输出可能偷吃到谷子的牛的位子,(按输入的先后顺序输出)。
解题思路:明显的最短路径题,用Dijstra算法。
AC代码:
Accepted 1160K 0MS C++ 1063B 2010-11-10 21:31:57

#include <iostream>
#include <queue>
using namespace std;
const int MAX=501;
const int INF=99999999;
int map[MAX][MAX],path[MAX],hash[MAX];
int F,P,C,M;
void Dijkstra(int s,int n)
{
 int i,t;
 queue<int>Q;
 Q.push(s);
 path[s]=0;
 hash[s]=false;
 while(!Q.empty())//当所有点都处理过为止
 {
  t=Q.front();
  Q.pop();
  for(i=0;i<=F;i++)
  {
   if(map[t][i]!=INF&&path[i]>path[t]+map[t][i])
   {
    path[i]=path[t]+map[t][i];//无论该点是不是已被处理过,都要更新
    if(hash[i])//如果没被处理过就放进队列里
    {
     hash[i]=false;
     Q.push(i);
    }
   }
  }
 }
}
int main ()
{
 int i,j,a,b,time,ans[101];
 scanf("%d%d%d%d",&F,&P,&C,&M);
 for(i=0;i<=F;i++)//初始化
 {
  for(j=0;j<=F;j++)
  {
   map[i][j]=INF;//用邻接矩阵储存F个结点
   map[i][i]=0;//自身到自身的距离为0
   hash[i]=true;//用来标记该点是否已被处理过
   path[i]=INF;//记录i点到起始点s的最短路径
  }
 }
 for(i=1;i<=P;i++)
 {
  scanf("%d%d%d",&a,&b,&time);
  //a--;b--;
  if(time<map[a][b])//可能有重边
  {map[a][b]=map[b][a]=time;}
 }
 Dijkstra(1,F);//Dijkstra(0,F);
 int r=0,c;
 for(i=1;i<=C;i++)
 {
  scanf("%d",&c);
  if(path[c]<=M)//if(path[c-1]<=M)
   ans[r++]=i;
 }
 printf("%d/n",r);
 for(i=0;i<r;i++)
  printf("%d/n",ans[i]);
 return 0;
}

你可能感兴趣的:(Integer,Path,each,reference,output,traversal)