hnuoj 13313 Smoking gun(SPFA+拓扑序)

Smoking gun
Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB
Total submit users: 11, Accepted users: 3
Problem 13313 : No special judgement
Problem description

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 n (2 ≤ n ≤ 100) and m (1 ≤ m ≤ 1000): the number of people involved and the number of observations.

• n lines with a string S, consisting of up to 20 lower and upper case letters, and two integers x and y (0 ≤ x,y ≤ 1000000): the unique identifier for a person and his/her position in Cartesian coordinates, in metres from the origin.

• m lines of the form “S1 heard S2 firing before S3”, where S1, S2 and S3 are identifiers among the people involved, and S2 6= 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−7in 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 com- patible with the observations, output “IMPOSSIBLE” instead.


Sample Input
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
Sample Output
BillyTheKid John
IMPOSSIBLE
UNKNOWN
http://acm.hnu.cn/online/?action=problem&type=show&id=13313&courseid=0


给出n个人的位置坐标 给出这些人中一些人听到枪声的先后关系 问能不能确定开枪的顺序

首先是因为距离的远近产生了听到枪声的先后关系 所以这里有一个不等式的关系 列出m个不等式 判断这些不等式里面有没有解  如果有解就判断有几个解  

用SPFA来处理不等式关系 差分约束  然后建图 使用拓扑序判断有几个解。。如果有唯一解就输出那个顺序

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <map>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 110
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int n,m;
struct point
{
    double x,y;
}po[220];
map<string,int> M;

double caldis(int u,int v)
{
    return sqrt((po[u].x-po[v].x)*(po[u].x-po[v].x)+(po[u].y-po[v].y)*(po[u].y-po[v].y));
}

struct Edge
{
    int v;
    double cost;
    Edge(int _v,double _cost):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN];
void addedge(int u,int v,double w)
{
    E[u].push_back(Edge(v,w));
}
bool vis[MAXN];
int cnt[MAXN];
double dis[MAXN];
int rd[MAXN],v[MAXN];
vector<int> pic[MAXN];
string sss[MAXN];
bool SPFA(int s)
{
    MEM(vis,0);
    for(int i=1;i<=n;i++)
        dis[i]=1000000000000.0;
    vis[s]=1;
    dis[s]=0;
    queue<int> que;
    while(!que.empty()) que.pop();
    que.push(s);
    MEM(cnt,0);
    cnt[s]=1;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=0;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            if(dis[v]>dis[u]+E[u][i].cost)
            {
                dis[v]=dis[u]+E[u][i].cost;
                if(!vis[v])
                {
                    vis[v]=1;
                    que.push(v);
                    if(++cnt[v]>n)
                        return 0;
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(dis[i]<-eps)
        {
            pic[i].push_back(s);
            rd[s]++;
        }
    }
    return 1;
}

int main()
{
//    fread;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        scanf("%d%d",&n,&m);
        getchar();
        for(int i=1;i<=n;i++)
        {
            char s[30];
            scanf("%s",s);
            sss[i]=s;
            M[s]=i;
            scanf("%lf%lf",&po[i].x,&po[i].y);
            pic[i].clear();
            E[i].clear();
        }
        MEM(v,0); MEM(rd,0);
        while(m--)
        {
            char ch[30],str[30];
            scanf("%s",str);
            getchar();
            int x=M[str];
            scanf("%s",ch);
            getchar();
            scanf("%s",str);
            getchar();
            int y=M[str];
            scanf("%s",ch);
            getchar();
            scanf("%s",ch);
            getchar();
            scanf("%s",str);
            getchar();
            int z=M[str];
//            cout<<"xx  "<<x<<"   yy  "<<y<<"  zz  "<<z<<endl;
            double dis1=caldis(x,y);
            double dis2=caldis(x,z);
            v[y]=v[z]=1;
            addedge(z,y,dis2-dis1);
        }
        int flag=0;

        for(int i=1;i<=n;i++)
        {
            if(!SPFA(i))
            {
                flag=1;
                puts("IMPOSSIBLE");
                break;
            }
        }
        if(flag)
            continue;
        flag=0;
        queue<int> que;
        while(!que.empty()) que.pop();
        for(int i=1;i<=n;i++)
            if(!rd[i]&&v[i])
                que.push(i);
        vector<int> ans;
        while(!que.empty())
        {
            if(que.size()>1)
            {
                puts("UNKNOWN");
                flag=1;
                break;
            }
            int u=que.front(); que.pop();
            ans.push_back(u);
            for(int i=0;i<pic[u].size();i++)
            {
                int v=pic[u][i];
                rd[v]--;
                if(rd[v]==0) que.push(v);
            }
        }
        while(!que.empty()) que.pop();
        if(flag)
            continue;
        for(int i=0;i<ans.size();i++)
        {
            if(i)
                printf(" ");
            cout<<sss[ans[i]];
        }
        puts("");
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值