10986-Sending email // Bellman-Ford

Source: Internet
Author: User

Question analysis: the bare shortest path, but there are a lot of nodes. The adjacent matrix will be RE, and the graph will be stored in the adjacent linked list. Then BallmanFord went on.

Vomit: first, I didn't read the data and RESS all the time. Then, when I output the data, I reversed it with the number. I 've been WA, wow!

Code:

#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int INF = 2000000010;const int maxn = 2*50005;int u[maxn],v[maxn],w[maxn];int first[maxn],next[maxn];int n,m,s,t;queue<int> q;bool inq[maxn];int d[maxn];void Bellman_Ford(){    for(int i = 0; i < n; i++) d[i] = INF,inq[i] = false;    d[s] = 0;    q.push(s); inq[s] = true;    while(!q.empty())    {        int x = q.front(); q.pop();        inq[x] = false;        for(int e = first[x]; e != -1; e = next[e])        {            if(d[v[e]] > d[x] + w[e])            {                d[v[e]] = d[x] + w[e];                if(!inq[v[e]])                {                    inq[v[e]] = true;                    q.push(v[e]);                }            }        }    }}int main(){    int N;    scanf("%d",&N);    for(int k = 1; k <= N; k++)    {        scanf("%d%d%d%d",&n,&m,&s,&t);        for(int i = 0; i < n; i++) first[i] = -1;        for(int e = 0,i = 0; i < m; e += 2,i++)        {            scanf("%d%d%d",&u[e],&v[e],&w[e]);            next[e] = first[u[e]];            first[u[e]] = e;            u[e+1] = v[e];            v[e+1] = u[e];            w[e+1] = w[e];            next[e+1] = first[v[e]];            first[v[e]] = e + 1;        }        Bellman_Ford();        printf("Case #%d: ",k);        if(d[t] != INF) printf("%d\n",d[t]);        else printf("unreachable\n");    }    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.