(intermediate) 最短路(差分约束+拓扑排序) UVA 1516 - Smoking gun
来源:互联网 发布:精华液推荐 知乎 编辑:程序博客网 时间:2024/06/05 07:38
1516 - Smoking gun
Time limit: 10.000 secondsAndy: "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 (2n100) and m (1m1000): 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 (0x, y1000000): 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 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
#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();}}
- (intermediate) 最短路(差分约束+拓扑排序) UVA 1516 - Smoking gun
- uva 1516 Smoking gun(差分约束系统)
- UVALive - 5906 Smoking gun(差分约束系统+拓扑排序)
- UVA 1516 Smoking gun 查分约束+拓扑
- UVALive 5906 Smoking gun(差分约束 + toposort)
- Hoj 13313 Smoking gun 差分约束问题
- 最短路(4)--差分约束
- poj 3169 Layout(差分约束求最短路)
- 差分约束系统详解(转化为最短路)
- POJ 3159 Candies(spfa最短路,差分约束)
- poj3159 {差分约束系统—>最短路)
- poj3159 Candies(差分约束,最短路)
- poj 3169 Layout(差分约束,最短路)
- hdu 3592 World Exhibition(差分约束,最短路)
- POJ 3169 Layout (差分约束最短路)
- 最短路&&差分约束
- UVA 11478 - Halum(差分约束+最短路)
- hnuoj 13313 Smoking gun(SPFA+拓扑序)
- (beginer) 最短路 UVA 10246 Asterix and Obelix
- (beginer) 最短路 UVA 558 Wormholes
- REDHAT(LINUX) 扩 /HOME 所在LV
- (beginer) 最短路 UVA 658 It\'s not a Bug, it\'s a Feature!
- (intermediate)最短路(最长路) UVA 1348 - Tomato Automata
- (intermediate) 最短路(差分约束+拓扑排序) UVA 1516 - Smoking gun
- (intermediate)次短路 UVA 10342 - Always Late
- (intermediate) 最短路(第k短路) UVA 10740 - Not the Best
- (advanced) 最短路 UVA 12227 Wormholes
- (beginer) 最小生成树 UVA 1395 Slim Span
- CSDN感谢你!
- (beginer) 最小生成树 UVA 10600 ACM Contest and Blackout
- (beginer) 最小生成树 UVA 11747 Heavy Cycle Edges
- (beginer) 最小生成树 UVA 544 Heavy Cargo