UVALive-5906 Smoking Gun (differential constrained system + topological sequencing)

Source: Internet
Author: User

The main topic: give n personal coordinates and m to hear the order of gunfire, ask if there is a shooting order to meet the m conditions, is a or how much?

Problem-solving ideas: A typical differential constraint system, set I personal firing time for TI
Suppose a hears a B's gunshot, and then hears the C gunshot.
Then we need to meet a condition.
TB + DIS (A, b) <= tc + DIS (a,c) (this is supposed to be divided by the Sonic, but omitted here)
By this inequality constructs the edge, the concrete construction does not say, understands the differential restraint system the metropolis constructs, if not, proves that you do not know very much
Then judge, if there is a ring, that the condition cannot be fully established
In the case of conditions, topology, if the topology fails, indicating that there are multiple shooting order, if successful, there is only one

#include <cstdio>#include <cstring>#include <iostream>#include <map>#include <vector>#include <cmath>#include <queue>#include <set>using namespace STD;#define N#define M 1010structnode{stringSintx, y;} Node[n];structedge{intU, V, next;DoubleC;} E[M]; Map<string,int>Name vector<int>Vec[n]; Set<int>Tuopu;intN, m, tot;intHead[n];CharStr1[n], Str2[n], str3[n];voidAddedge (intUintVDoublec) {e[tot].u = u; E[TOT].V = v; E[TOT].C = C; E[tot].next = Head[u]; Head[u] = tot++;}DoubleDistanceintIintj) {intx = node[i].x-node[j].x;inty = node[i].y-node[j].y;return sqrt(1.0* x * x +1.0* y * y);}voidInit () {scanf("%d%d", &n, &m);    Name.clear (); Tuopu.clear ();stringS1, S2, S3;intx, y; for(inti =1; I <= N; i++) {Cin>> node[i].s >> node[i].x >> node[i].y;         NAME[NODE[I].S] = i;    Vec[i].clear (); }memset(Head,-1,sizeof(head)); tot =0;//U + dist1 <= v + dist2     for(inti =0; I < m; i++) {scanf('%s heard%s firing before%s ', str1, str2, STR3); S1 = str1; s2 = str2; s3 = STR3;DoubleDist1 = Distance (Name[s2], name[s1]);DoubleDist2 = Distance (Name[s3], name[s1]);intU = name[s2];intv = name[s3];        Tuopu.insert (U);        Tuopu.insert (v);    Addedge (V, U, dist2-dist1); }}#define INF 0x3f3f3f3f#define ESP 1e-8DoubleD[n];BOOLVis[n];intIn[n], cnt[n];BOOLSPFA (ints) {memset(CNT,0,sizeof(CNT));memset(Vis,0,sizeof(VIS)); for(inti =1; I <= N; i++) D[i] = INF; Queue<int>Q; D[s] =0; Q.push (s); while(! Q.empty ()) {intU = Q.front (); Q.pop (); for(inti = Head[u]; ~i; i = e[i].next) {intv = e[i].v;if(D[v] > D[u] + ESP + e[i].c) {D[v] = D[u] + e[i].c;if(!vis[v]) {Vis[v] =true; Q.push (v);if(++cnt[v] >= N)return false; }}} Vis[u] =false; } for(inti =1; I <= N; i++)if(D[i] <-ESP)            {Vec[i].push_back (s);            in[s]++;        Tuopu.erase (s); }return true;}voidSolve () {memset(In,0,sizeof(in)); for(inti =1; I <= N; i++)if(!SPFA (i)) {printf("impossible\n");return; } Queue<int>Ans while(Tuopu.size ()) {if(Tuopu.size () >1) {printf("unknown\n");return; }intt = *tuopu.begin ();        Ans.push (t); Tuopu.erase (t); for(inti =0; I < vec[t].size (); i++) {--in[vec[t][i]];if(In[vec[t][i]] = =0) Tuopu.insert (Vec[t][i]); }    }cout<< Node[ans.front ()].s; Ans.pop (); while(!ans.empty ()) {cout<<" "<< Node[ans.front ()].s;    Ans.pop (); }printf("\ n");}intMain () {intTestscanf("%d", &test); while(test--)        {init ();    Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVALive-5906 Smoking Gun (differential constrained system + topological sequencing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.