Poj 2983 is the information reliable? (Differential constraint system)

Source: Internet
Author: User

Address: poj 2983

At the beginning of this question, I have no idea what relationship it has with the differential constraint system ..... Later, we found that we only needed to determine a negative ring ..

If there is a conflict, a negative ring will be formed. The reason why a positive value is added to the graph is that, in this case, data such as 1 2 4 and 1 2 3 will form a negative ring. This method is still clever... Then, for those unclear positions of V, an inequality will be formed together with those equations of P, and relaxation will be used to solve the problem in the process of judging the ring with the shortest short circuit.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;int d[2000], head[2000], cnt, n;struct node{    int u, v, w, next;} edge[200000];void add(int u, int v, int w){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;}void berman(){    memset(d,INF,sizeof(d));    int i, j, flag, k;    for(i=1; i<n; i++)    {        flag=0;        for(j=0; j<=n; j++)        {            for(k=head[j]; k!=-1; k=edge[k].next)            {                int v=edge[k].v;                if(d[v]>d[j]+edge[k].w)                {                    d[v]=d[j]+edge[k].w;                    flag=1;                }            }        }        if(!flag)            break;    }    flag=0;    for(i=0; i<cnt; i++)    {        if(d[edge[i].v]>d[edge[i].u]+edge[i].w)        {            flag=1;            break;        }    }    if(flag)        printf("Unreliable\n");    else        printf("Reliable\n");}int main(){    int m, a, b, c, i;    char ch;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(head,-1,sizeof(head));        cnt=0;        while(m--)        {            getchar();            scanf("%c",&ch);            if(ch=='P')            {                scanf("%d%d%d",&a,&b,&c);                add(a,b,-c);                add(b,a,c);            }            else            {                scanf("%d%d",&a,&b);                add(a,b,-1);            }        }        berman();    }    return 0;}




Poj 2983 is the information reliable? (Differential constraint system)

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.