PKU 2394-checking an alibi

Source: Internet
Author: User
Checking an alibi
Time limit:1000 ms Memory limit:65536 K
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 cocould 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 cocould be guilty of the crime.

* Lines 2. n + 1: A single cow number on each line that is one of the cows that cocould be guilty of the crime. The list must be in ascending order.

Sample Input

7 6 5 81 4 21 2 12 3 63 5 55 4 61 7 914537

Sample output

41234

Hint

Input details:

Fields/distances like this:

          6
4------5
| |
2| |
| |
7-----1 |5
9 | |
1| |
| |
2------3

Output details:

Any cow should t cow 5 cowould have done it. Cow 5 wowould take 9 seconds to get to the barn.

Source

Usaco 2005 March Silver: There are f farm in one place, namely F1, F2,..., FP. Among them, F1 is a barn! Some C cows want to steal grain from the barn on these farm. However, a camera just captured the position M seconds before they got the grain. And it is known that there are P paths between these farm, for example, there is a path connection between FI and FJ, and know the time when this path is taken (there may be multiple paths between FI and fj ). Tell you the position of the C-headed ox M seconds ago, and ask you to output the cow's seat that may steal the grain (output in the input order ). Solution: the obvious shortest path problem is solved using the dijstra algorithm. AC code:

Accepted 1160 K 0 ms C ++ Ipv3b 2010-11-10 21:31:57


# Include <iostream>
# Include <queue>
Using namespace STD;
Const int max = 501;
Constint 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 () // when all vertices have been processed
{
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]; // update whether the vertex has been processed or not.
If (hash [I]) // put it in the queue if it has not been processed
{
Hash [I] = false;
Q. Push (I );
}
}
}
}
}
Int main ()
{
Int I, j, A, B, time, ANS [101];
Scanf ("% d", & F, & P, & C, & M );
For (I = 0; I <= f; I ++) // Initialization
{
For (j = 0; j <= f; j ++)
{
Map [I] [J] = inf; // store F nodes using the adjacent matrix
Map [I] [I] = 0; // The distance from itself to itself is 0
Hash [I] = true; // used to mark whether the vertex has been processed
Path [I] = inf; // record the shortest path from point I to start point S
}
}
For (I = 1; I <= P; I ++)
{
Scanf ("% d", & A, & B, & time );
// A --; B --;
If (time <map [a] [B]) // There may be duplicate Edges
{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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.