(intermediate) 最短路(差分约束+拓扑排序) UVA 1516 - Smoking gun

来源:互联网 发布:精华液推荐 知乎 编辑:程序博客网 时间:2024/06/05 07:38

1516 - Smoking gun

Time limit: 10.000 seconds

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$ \le$n$ \le$100) and m (1$ \le$m$ \le$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$ \le$x, y$ \le$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 $ \neq$ 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 Input 

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

Sample Output 

BillyTheKid JohnIMPOSSIBLE 
UNKNOWN 

题意:题目给出各个人的位置,然后给出某些人听到枪声的前后关系,这里面要考虑声音的传播速度。问能不能根据给出的关系确定开枪的顺序。

思路:首先我们设每个人开枪的时间为ti,那么根据上面的关系能列出形如ta-tb<=k 这样的不等式,那么这个就是一个差分约束系统,所以根据这个建图,用spfa看有没有环路,有环路代表这些不等式无解。 如果都没有环,那么按照拓扑排序的过程,如果每一次只有一个入度为0,那么就按照这个顺序输出,如果出现多个入度为0,代表答案不唯一,到这里,这个题目就解决了。

代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<math.h>#include<map>#include<vector>#include<queue>#include<set>#include<string>using namespace std;#define eps 1e-8#define LL long longconst int inf = 1e9;const int maxn = 110;double t[maxn];int cnt[maxn] , ind[maxn] , size;vector<int> G[maxn];bool inq[maxn];set<int> No;char person[maxn][30];char buffer[30];int n , m , ptr;struct Point{Point(int xx=0, int yy=0, int nn=0) : x(xx) , y(yy) , no(nn) { }int x , y;int no;};map<string,Point> name;struct Node{int v;double w;Node * next;} edge[2000] , *first[maxn];void init(){ptr = 0;memset(first,0,sizeof(first));}void add(int x,int y,double w){edge[++ptr].v = y;edge[ptr].w = w;edge[ptr].next = first[x];first[x] = &edge[ptr];}inline LL sqr(LL x){return x*x;}double dist(const Point & p1,const Point & p2){return sqrt (sqr(p1.x-p2.x) + sqr(p2.y-p1.y));}void input(){for (int i = 1 ; i <= n ; ++i) G[i].clear();memset(ind,0,sizeof(ind));string s;No.clear();name.clear();for (int i = 1 ; i <= n ; ++i){int x , y;scanf("%s%d%d",person[i],&x,&y);s = person[i];name[s] = Point(x,y,i);}string s1 , s2 , s3;while (m--){scanf("%s%*s%",buffer); s1 = buffer;scanf("%s%*s%*s",buffer); s2 = buffer;scanf("%s",buffer); s3 = buffer;Point p1 , p2 , p3;p1 = name[s1];p2 = name[s2];p3 = name[s3];double d1 = dist(p3,p1);double d2 = dist(p2,p1);int u , v;u = p3.no , v = p2.no;No.insert(u);No.insert(v);add(u,v,d1-d2);}size = No.size();}bool spfa(int s){memset(cnt,0,sizeof(cnt));memset(inq,false,sizeof(inq));for (int i = 1 ; i <= n ; ++i) t[i] = inf;queue<int> q;q.push(s);t[s] = 0;inq[s] = true;while (q.size()){int u = q.front(); q.pop();inq[u] = false;int v ; double w;for (Node * p = first[u] ; p ; p=p->next){v = p->v; w = p->w;if (t[v] > t[u]+w+eps) {t[v] = t[u]+w;if (inq[v]) continue;inq[v] = true;q.push(v);++cnt[v];if (cnt[v] >= size) return false;}}}for (int i = 1 ; i <= n ; ++i) if (t[i] < -eps) {G[i].push_back(s);++ind[s];No.erase(s);}return true;}void solve(){for (int i = 1 ; i <= n ; ++i)  if (!spfa(i)){ printf("IMPOSSIBLE\n"); return;}queue<int> ans;while (No.size()) {if (No.size() > 1) { printf("UNKNOWN\n"); return; }int x = *No.begin();ans.push(x);No.erase(No.begin());for (int i = 0 ; i < G[x].size() ; ++i) {--ind[G[x][i]];if (ind[G[x][i]]==0) No.insert(G[x][i]);}}printf("%s",person[ans.front()]); ans.pop();while (ans.size()) { printf(" %s",person[ans.front()]); ans.pop(); }cout << endl;}int main(){int T; cin>>T;while (T--){scanf("%d%d",&n,&m);init();input();solve();}}
0 0
原创粉丝点击