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!” |
Input |
On the first line a positive integer: the number of test cases, at most 100. After that per test case: |
Output |
Per test case: |
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 |
给出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;
}