Poj 1364 King differential constraint finding negative ring

Source: Internet
Author: User

Well, although it's a water question + template question, I still learned a lot and record it.

First, the inequality given in the question is less than, but the difference constraint system can only handle situations where the value is less than or equal to, so it is necessary to convert it into a situation where the value is less than or equal. The integer processing method is very simple =

The next step is to find the negative ring. In fact, you don't need to consider the graph connection. You just need to set the D of all vertices to 0 at the beginning and then push them into the queue.

PS: This method can also be used to deal with the problem of the shortest path of multiple source points.

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;const int maxn = 105;const int maxm = 5005;int first[maxn],nxt[maxm],v[maxm],w[maxm];int d[maxn],n,m,ecnt,cnt[maxn];bool inq[maxn];void adde(int _u,int _v,int _w) {    v[ecnt] = _v; w[ecnt] = _w;    nxt[ecnt] = first[_u];    first[_u] = ecnt;    ecnt++;}void solve() {    bool bad = false;    queue<int> q;    for(int i = 0;i <= n;i++) {        d[i] = 0;        cnt[i] = 1;        inq[i] = true;        q.push(i);    }    while(!q.empty() && !bad) {        int x = q.front(); q.pop();        inq[x] = false;        for(int i = first[x];i != -1;i = nxt[i]) {            if(d[v[i]] > d[x] + w[i]) {                if(!inq[v[i]]) {                    inq[v[i]] = true;                    cnt[v[i]]++;                    q.push(v[i]);                    if(cnt[v[i]] > n + 2) {                        bad = true;                        break;                    }                }                d[v[i]] = d[x] + w[i];            }        }    }    if(bad) puts("successful conspiracy");    else puts("lamentable kingdom");}int main() {    while(scanf("%d",&n),n) {        ecnt = 0;        char sig[16];        scanf("%d",&m);        memset(first,-1,sizeof(first));        memset(nxt,-1,sizeof(nxt));        for(int i = 0;i < m;i++) {            int a,b,c;            scanf("%d %d %s %d",&a,&b,sig,&c);            if(sig[0] == ‘g‘) adde(b + a,a - 1,-c - 1);            else adde(a - 1,b + a,c - 1);        }        /*        for(int i = 1;i <= n;i++) {            adde(i,i - 1,0);        }        */        solve();    }    return 0;}

  

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.