Poj 2394 checking an alibi (Shortest Path + Dijkstra)

Source: Internet
Author: User
Checking an alibi
Time limit:1000 ms   Memory limit:65536 K
Total submissions:6217   Accepted:2257

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

Q: I have f farms with C heads of cattle, and P's two-way routes provide the location of the farm where c heads of cattle were located M seconds ago. Ask M who might have arrived at 1 farm to steal food.
Idea: It's a pure short circuit problem. Find the distance from every cow to one farm and compare it with M.
Code: 16 ~ 32 Ms
I made a fully functional template.
# Include <iostream> # include <algorithm> # include <stdio. h> # include <string. h> using namespace STD; # define M 501 # define INF 999999 # define min (a, B) (a <B? A: B) int map [m] [m], DIS [m]; int P [110]; int N, M, F, P, C; void Dijkstra () // algorithm core. {Bool cov [m]; memset (COV, 0, sizeof (COV); For (INT I = 1; I <= f; I ++) dis [I] = (I = 1? 0: INF); For (INT I = 1; I <= f; I ++) // I throw the starting point, but I just take one more loop. {Int min = inf, s; For (INT y = 1; y <= f; y ++) if (! Cov [y] & dis [y] <min) {min = dis [y]; S = y;} cov [s] = 1; for (INT y = 1; Y <= f; y ++) dis [y] = min (DIS [Y], DIS [s] + map [s] [Y]);} void Init () // input. {Int I, j, A, B, Z; for (I = 0; I <m; I ++) for (j = 0; j <m; j ++) map [I] [J] = (I = J? 0: INF); scanf ("% d", & F, & P, & C, & M); for (I = 1; I <= P; I ++) {scanf ("% d", & A, & B, & Z ); map [a] [B] = map [B] [a] = min (Map [a] [B], Z);} for (I = 1; I <= C; I ++) scanf ("% d", & P [I]);} void output () // output. {Int I, j, n = 0; for (I = 1; I <= C; I ++) if (DIS [p [I] <= m) {n ++; P [I] =-1;} printf ("% d \ n", n); for (I = 1; I <= C; I ++) if (P [I] =-1) printf ("% d \ n", I) ;}int main () {Init (); dijkstra (); output (); 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.