hdu1317 (SPFA judgment positive ring +floyd judgment Unicom)

Source: Internet
Author: User

Test instructions is well understood, judging whether it can go from the starting point to the end point, each to a few points will be obtained this point represents the energy value, the starting point itself with 100 energy value.

Just began to write a bare spfa, timed out, found that there may be from the starting point can not go to the end, and there is a positive ring, so that the program will never end, corrected with Floyd to determine whether the beginning and end of the link, and then use the SPFA to find the longest road, encounter the point in the ring, the point in the ring can The output "winnable" can be reached, because an infinite energy value can be obtained in the positive loop. Can not continue to do SPFA, and the point in the ring no longer into the queue, this change is destroyed, so even if there are multiple rings in the diagram can be processed. If no points in the ring can reach the end point, determine whether the value of the corresponding end point in the Dist array is greater than 0.

#include <iostream> #include <queue> #include <memory.h> #include <vector> #include <stdio.h >using namespace std;vector<vector<int> > v;vector<int> energy;bool a[105][105];int N;void Floyd (                 {for (int k=0;k<n;k++) {for (int. i=0;i<n;i++) {for (int j=0;j<n;j++) {            if (A[i][k]&&a[k][j])//update Unicom a[i][j]=true;    }}}}bool SPFA (int s,int E) {vector<int> dist;    Vector<int> counter;    Counter.resize (N);    Dist.resize (N);        for (int i=0;i<n;i++) {dist[i]=0;    counter[i]=0;    } dist[s]=100;    Queue<int> Q;    Q.push (S);    counter[s]++; while (!        Q.empty ()) {int S=q.front ();        Q.pop ();            for (int i=0;i<v[s].size (); i++) {int e=v[s][i];                if (Dist[s]+energy[e]>dist[e]) {dist[e]=dist[s]+energy[e]; if (counter[E]<n) {Q.push (e);                counter[e]++; } else if (Counter[e]>=n&&a[e][n-1]) {return true;//this point does not enter the queue, thereby Destroy the positive ring.                and continue to execute the SPFA to prevent the occurrence of a second ring can be reached. }}}} return dist[n-1]>0;//to determine whether to go through the positive loop (it is possible that there is no ring in the diagram or unreachable through the ring).        }int Main () {while (scanf ("%d", &n) &&n!=-1) {memset (a,false,sizeof (A));        V.clear ();        V.resize (N);        Energy.clear ();            for (int i=0;i<n;i++) {int t,a;            scanf ("%d", &t);            Energy.push_back (t);            scanf ("%d", &t);                for (int j=0;j<t;j++) {scanf ("%d", &a);                V[i].push_back (A-1);            A[i][a-1]=true;        }} Floyd (); if (!        A[0][n-1]) cout<< "hopeless" <<endl; else if (SPFA (0,n-1)) cout<< "winnable" <<endl    else cout<< "hopeless" <<endl; } return 0;}

  

hdu1317 (SPFA judgment positive ring +floyd judgment Unicom)

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.