Hdu3592 World Exhibition --- difference Constraint

Source: Internet
Author: User

Hdu3592 World Exhibition --- difference Constraint

There is nothing special about this question.

X conditions: Sb-Sa <= c

Y conditions: Sa-Sb <=-c

The question is the relationship between 1 and n.

If there is a negative ring, the entire process is impossible. Output-1

If the graph is connected (1 to n are connected), Output d [n]

Disconnections are the case where-2 is mentioned in the question.


In the past, we usually add an additional node for creating a graph, or we start to team all the nodes. This means that we can add a meaningless condition considering the problem of disconnectivity.


#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include #include 
      
       #include 
       
        #include 
        #define inf 0x3f3f3f3f#define eps 1e-6#define ll __int64using namespace std;#define N 1010struct node{ int v,w,next;}e[30020];int d[N],inq[N],outq[N],n,head[N],h;void addedge(int a,int b,int c){ e[h].v=b; e[h].w=c; e[h].next=head[a]; head[a]=h++;}int spfa(int s){ memset(d,0x3f,sizeof d); memset(inq,0,sizeof inq); memset(outq,0,sizeof outq); d[s]=0;inq[s]=1; queue
         
           q; q.push(s); int i,x; while(!q.empty()) { x=q.front(); q.pop(); inq[x]=0; outq[x]++; if(outq[x]>n) return 0; for(i=head[x];i!=-1;i=e[i].next) { if(d[e[i].v]>d[x]+e[i].w) { d[e[i].v]=d[x]+e[i].w; if(!inq[e[i].v]) { inq[e[i].v]=1; q.push(e[i].v); } } } } return 1;}void init(){ memset(head,-1,sizeof head); h=0;}int main(){ int T,a,b,c,x,y; scanf("%d",&T); while(T--) { init(); scanf("%d%d%d",&n,&x,&y); while(x--) { scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); } while(y--) { scanf("%d%d%d",&a,&b,&c); addedge(b,a,-c); } if(!spfa(1)) printf("-1\n"); else if(d[n]!=inf) printf("%d\n",d[n]); else printf("-2\n"); } return 0;}
         
       
      
     
    
   
  
 


Related Article

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.