HDU 3592 Check constraint + judgment ring

Source: Internet
Author: User

Click to open link

Test instructions: There are n individuals, and then x relationship and y relationship, the x relationship represents that the distance of these two people can not exceed c,y represents the distance of these two people is greater than or equal to C, if not meet all the output-1, if the position of 1 and n can be infinitely large output-2, otherwise the maximum distance to output two people

Idea: is to check the model of the constraint, x relationship according to the location of the building edge, Y is directly by the location of the line, that is, two initial direction is different, then there is a negative ring output-1, distance infinity output-2, otherwise is dis[n] value on the line, you can come out with a discussion

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <
Algorithm> using namespace std;
typedef long Long LL;
const int inf=0x3f3f3f3f;
const int maxn=20010;
struct edge{int from,to,cost;};
Edge ES[MAXN];
ll DIS[MAXN];
int v,e;
    void Shortest_path (int s) {for (int i=0;i<=v;i++) Dis[i]=inf;
    dis[s]=0;
        while (1) {bool update=0;
            for (int i=0;i<e;i++) {edge e=es[i];
                if (dis[e.from]!=inf&&dis[e.to]>dis[e.from]+e.cost) {dis[e.to]=dis[e.from]+e.cost;
            update=1;
    }} if (!update) break;
    }} bool Find_negative_loop () {memset (dis,0,sizeof (dis));
            for (int i=0;i<v;i++) {for (int j=0;j<e;j++) {edge e=es[j];
                if (dis[e.to]>dis[e.from]+e.cost) {dis[e.to]=dis[e.from]+e.cost;
            if (I==v-1) return 1; }}} return0;
    } int main () {int t,x,y,a,b,c;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d%d", &v,&x,&y);
        E=x+y;
        int k=0;
            while (x--) {scanf ("%d%d%d", &a,&b,&c);
            if (a<b) {es[k].from=a;es[k].to=b;es[k++].cost=c;}
        else {es[k].from=a;es[k].to=b;es[k++].cost=-c;}
            } while (y--) {scanf ("%d%d%d", &a,&b,&c);
            if (b<a) {es[k].from=b;es[k].to=a;es[k++].cost=c;}
        else {es[k].from=b;es[k].to=a;es[k++].cost=-c;}
            } if (Find_negative_loop ()) {printf (" -1\n");
        Continue
        } shortest_path (1);
        if (dis[v]==inf) printf (" -2\n");
    else printf ("%d\n", Dis[v]);
} 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.