UVA1516 smoking gun

Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [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 survived (although there were serious injuries), but nobody could 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 integers (2 ≤ ≤ 100) and (1 ≤ ≤ 1 000): the number of people involved and the number of observations.
  • lines with a string S, consisting of up to 20 lower and upper case letters, and two integers and (0 ≤ x,y ≤ 1 000 000): the unique identifier for a person and his/her position in Cartesian coordinates, in metres from the origin.
  • lines of the form “S1 heard S2 firing before S3”, where S1, S2 and S3 are identifiers among the people involved, and S2≠S3.

If a person was never mentioned as S2 or S3, 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

3
4 2
BillyTheKid 0 0
Andy 10 0
John 19 0
Larry 20 0
Andy heard BillyTheKid firing before John
Larry heard John firing before BillyTheKid
2 2
Andy 0 0
Beate 0 1
Andy heard Beate firing before Andy
Beate heard Andy firing before Beate
3 1
Andy 0 0
Beate 0 1
Charles 1 3
Beate heard Andy firing before Charles
BillyTheKid John
IMPOSSIBLE
UNKNOWN
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



做练习赛的时候居然没有想到是差分约束,还以为是简单的拓扑,后来发现距离确实是个很关键的因素。


#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 long N = 105;
const int INF = 0x3f3f3f3f;

struct Node{
    long 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%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 %s %s %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;
/*如果对于i来说所有的j都满足dis[i][j]<0那么i就是最近的*/
        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;
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值