# Va 558-Wormholes // Bellman-Ford

Source: Internet
Author: User

Analysis: Template question, but understanding the question. The Ring cannot be a strong connected branch. I wrote a code based on the strong connected branch, but WA is now ....

The following is the AC code:

`#include<cstdio>#include<cstring>#include<queue>#include<vector>using namespace std;const int INF = 100000000;const int maxn = 1005;vector<int> G[maxn];int weight[maxn][maxn];queue<int> q;bool inq[maxn];int d[maxn];int count[maxn];int n,m;bool Bellman_Ford(){    for(int i = 0 ; i < n; i++) d[i] = INF,inq[i] = false,count[i] = 0;    d[0] = 0;    count[0] = 1;    q.push(0); inq[0] = true;    while(!q.empty())    {        int u = q.front(); q.pop();        inq[u] = false;        for(int i = 0; i < G[u].size(); i++)        {            int v = G[u][i];            if(d[v] > d[u] + weight[u][v])            {                d[v] = d[u] + weight[u][v];                if(!inq[v])                {                    inq[v] = true;                    count[v]++;                    if(count[u] >= n) return true;                    q.push(v);                }            }        }    }    return false;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i = 0; i < n; i++) G[i].clear();        int u,v,w;        for(int i = 0 ; i < m; i++)        {            scanf("%d%d%d",&u,&v,&w);            G[u].push_back(v);            weight[u][v] = w;        }        if(Bellman_Ford()) printf("possible\n");        else printf("not possible\n");    }    return 0;}`
Related Keywords:

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.