Hdu1874 unblocked project resumption

Source: Internet
Author: User

The spfa is really hard to write because of the short-circuit problem of the day. It can only be played once according to the template, and the loose operation is a bit clear, but it is still not clear, let me explain this question again, and then use the sta algorithm!

#include<iostream>#include<cstring>#include<algorithm>#include<queue>int n,m;const int inf = 0x3f3f3f3f;int v[2010],next[2010],w[2010];int first[210],d[210],inq[210],e;using namespace std;void init(){    e = 0;    memset(first,-1,sizeof(first));}void add_edge(int a,int b,int c){    v[e] = b;    next[e] = first[a];    w[e]= c;    first[a] = e++;}void spfa(int src){    queue<int> q;    memset(d,0x3f,sizeof(d));    d[src] = 0,inq[src] = 1;    q.push(src);    while(!q.empty()){        int u = q.front();        q.pop();        inq[u] = 0;        for(int i = first[u];i != -1;i = next[i]){            if(d[v[i]] > d[u]+w[i]){                d[v[i]] = d[u]+w[i];                if(!inq[v[i]])  q.push(v[i]),inq[v[i]] = 1;            }        }    }}int main(){    while(cin >> n>>m)    {        init();        for(int i =0;i < m;i++)        {            int a,b,c;            cin >> a>> b>> c;            add_edge(a,b,c);            add_edge(b,a,c);        }        int x,y;        cin >> x>> y;        spfa(x);        if(d[y] != inf)cout<< d[y]<< endl;        else cout<< "-1"<< endl;    }    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.