Uva1516 smoking gun

Source: Internet
Author: User
Tags creative commons attribution id3
Document directory
  • Smoking gun

Smoking gun
Time limit:6000 ms   Memory limit:Unknown   64bit Io format:% LLD & % LlU

[Submit]
[Go Back] [Status]

Description

Smoking gun

ANDY: "Billy the Kid fired first !"

LARRY: "no, I'm sure I heard the first shot coming from John !"

The arguments went back and forth during the trial after the big shoot-down, somewhere in the old wild west. miraculously, everybody had already ved (although there were serous injuries), but nobody cocould agree about the exact sequence of shots that had been
Fired. It was known that everybody had fired at most one shot, but everything had happened very fast. Determining the precise order of the shots was important for assigning guilt and penalties.

But then the sheriff, Willy the wise, interrupted: "look, I 've got a satellite image from the time of the shooting, showing exactly where everybody was located. as it turns out, Larry was located much closer to John than to Billy the Kid, while Andy was
Located just slightly closer to John than to Billy the Kid. thus, because sound travels with a finite speed of 340 meters per second, Larry may have heard John's shot first, even if Billy the Kid fired first. but, although Andy was closer to John than to Billy
The kid, he heard Billy the Kid's shot first-so we know for a fact that Billy the Kid was the one who fired first!

Your task is to write a program to deduce the exact sequence of shots fired in situations like the above.

 

Input

On the first line a positive integer: the number of test cases, at most 100. After that per test case:

  • One line with two integersN(2 ≤N≤ 100) andM(1 ≤M≤ 1 000): the number of people involved and the number of observations.
  • NLines with a stringS, Consisting of up to 20 lower and upper case letters, and two integersXAndY(0 ≤X, Y≤ 1 000 000): the unique identifier
    For a person and his/her position in Cartesian coordinates, in metres from the origin.
  • MLines of the form"S1HeardS2FiringBeforeS3", WhereS1,S2 andS3 are identifiers among the people involved, andS2≠ S3.

If a person was never mentionedS2 orS3, then it can be assumed that this person never fired, and only acted as a witness. No two persons are located in the same position.

The test cases are constructed so that an error of less than 10-7 in one distance calculation will not affect the output.

 

Output

Per test case:

  • One line with the ordering of the shooters that is compatible with all of the observations, formatted as the identifiers separated by single spaces.

If multiple distinct orderings are possible, output"Unknown"Instead. If no ordering is compatible with the observations, output"Impossible"Instead.

 

Sample in-and output

Input

Output

34 2BillyTheKid 0 0Andy 10 0John 19 0Larry 20 0Andy heard BillyTheKid firing before JohnLarry heard John firing before BillyTheKid2 2Andy 0 0Beate 0 1Andy heard Beate firing before AndyBeate heard Andy firing before Beate3 1Andy 0 0Beate 0 1Charles 1 3Beate heard Andy firing before Charles
BillyTheKid JohnIMPOSSIBLEUNKNOWN
Copyright Notice

This problem text is copyright by the nwerc 2011 jury. It is licensed under the Creative Commons Attribution-Share Alike license version 3.0; the complete license text can be found at: http://creativecommons.org/licenses/by-sa/3.0/legalcode

During the exercise competition, I did not think of the difference constraint, but thought it was a simple topology. Later I found that distance was indeed a key factor.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <cstring> # include <string> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace STD; const long n = 105; const int INF = 0x3f3f3f3f; struct node {long X, Y; char name [25];} node [105]; double Dis [N] [N]; bool flag [N]; int n, m; double getdis (int A, int B) {return SQRT (1.0 * (node [A]. x-node [B]. x) * (node [A]. x-node [B]. x) + (node [A]. y-node [B]. y) * (node [A]. y-node [B]. y);} int main () {INT cases; scanf ("% d", & cases); While (cases --) {memset (flag, false, sizeof (FLAG); scanf ("% d", & N, & M); For (INT I = 1; I <= N; I ++) {scanf ("% S % i64d % i64d", node [I]. name, & node [I]. x, & node [I]. y);} For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {if (I = J) dis [I] [J] = 0; else dis [I] [J] = inf;} Char S1 [25], S2 [25], S3 [25], TMP [25]; for (INT I = 1; I <= m; I ++) {scanf ("% S % s ", s1, TMP, S2, TMP, TMP, S3); int Id2 = 0, ID3 = 0, id1 = 0; For (INT I = 1; I <= N; I ++) {If (! Strcmp (S2, node [I]. Name) Id2 = I; If (! Strcmp (S3, node [I]. Name) ID3 = I; If (! Strcmp (S1, node [I]. name) id1 = I;} flag [Id2] = true; flag [ID3] = true; double Dist = getdis (id1, ID3)-getdis (id1, Id2 ); if (Dist <dis [Id2] [ID3]) dis [Id2] [ID3] = DIST;} For (int K = 1; k <= N; k ++) {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {If (DIS [I] [J]> dis [I] [k] + dis [k] [J]) dis [I] [J] = dis [I] [k] + dis [k] [J] ;}} int tot = 0; bool neg = false; for (INT I = 1; I <= N; I ++) if (Flag [I]) {tot ++; If (DIS [I] [I] <0) {neg = true; break ;}} // If (NEG) {printf ("impossible \ n"); continue;} int L [N]; bool mark; int CNT = 0; /* if all J Values of I meet dis [I] [J] <0, then I is the nearest */while (TOT --) {for (INT I = 1; I <= N; I ++) {If (! Flag [I]) continue; Mark = true; For (Int J = 1; j <= N; j ++) {if (I = J) continue; if (flag [J] & dis [I] [J]> = 0) {mark = false ;}} if (Mark) {L [CNT ++] = I; flag [I] = false; break;} If (! Mark) break;} If (Tot! =-1) {printf ("unknown \ n"); continue;} For (INT I = 0; I <cnt-1; I ++) {printf ("% s ", node [L [I]. name);} printf ("% s \ n", node [L [cnt-1]. name);} 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.