HDU 3259 Wormholes

Source: Internet
Author: User

HDU 3259 Wormholes

Meaning: Give you an n, m, and t n representing the number of points, and m representing the number of two-way edges t representing the wormhole, now I want you to determine whether you can traverse the past.

Wormhole means that the side is one-way and negative weight (the input is a positive number)




Idea: Can I traverse the past point, that is, whether there is a negative ring, and apply the template decisively? The dijkstra algorithm cannot detect the negative ring.

AC code:

#include
 
  #include
  
   #include
   
    #include
    
     #include#include
     
      using namespace std;#define maxn 520const int INF = 0x3fffffff;struct Edge{    int from,to,dist;};struct BellmanFord{    int n,m;    vector
      
        edges; vector
       
         G[maxn]; bool inq[maxn]; int d[maxn]; int p[maxn]; int cnt[maxn]; Edge e; void init(int n) { this->n=n; for(int i=0;i
        
         Q; memset(inq,0,sizeof(inq)); memset(cnt,0,sizeof(cnt)); for(int i=0;i
         
          d[u]+e.dist) { d[e.to]=d[u]+e.dist; p[e.to]=G[u][i]; if(!inq[e.to]) { Q.push(e.to); inq[e.to]=true; if(++cnt[e.to]>n) return true; } } } } return false; }};int main(){ int a,b,c,i,node,m,t,case1,k; bool j; scanf("%d",&case1); while(case1--) { scanf("%d %d %d",&node,&m,&t); if(node==0&&m==0)break; BellmanFord tu; tu.init(node); for(i=0;i
          
           

Related Keywords:

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.