【codevs 1557】热浪(SPFA)

来源:互联网 发布:js怎么获取对象的长度 编辑:程序博客网 时间:2024/04/27 03:05

SPFA

include<cstdio>#include<iostream>#include<queue>#include<algorithm>#include<cstring>#include<cmath>using namespace std;queue<int>q;int n,m,tot=0;const int maxn=6200+5;int first[maxn],next[maxn<<1];int dis[maxn];bool exi[maxn];struct edge{    int f,t,v;}es[maxn<<1];void build(int f,int t,int v){    es[++tot]=(edge){f,t,v};    next[tot]=first[f];    first[f]=tot;}bool cmp(edge a,edge b){    return a.v<b.v;}void spfa(int s){       memset(exi,0,sizeof(exi));    memset(dis,63,sizeof(dis));    while(!q.empty())        q.pop();    dis[s]=0;    q.push(s);    exi[s]=1;    while(!q.empty())    {        int top=q.front();        q.pop();        exi[top]=0;        for(int i=first[top];i;i=next[i])        {            if(dis[es[i].t]>dis[top]+es[i].v)            {                dis[es[i].t]=dis[es[i].f]+es[i].v;                if(!exi[es[i].t])                {                q.push(es[i].t);                exi[es[i].t]=1;                }            }        }    }}int main(){    int s,t;    int from,to,val;    scanf("%d%d%d%d",&n,&m,&s,&t);    for(int i=1;i<=m;i++)    {        scanf("%d%d%d",&from,&to,&val);        build(from,to,val);        build(to,from,val);    }    spfa(s);    printf("%d\n",dis[t]);    return 0;}
原创粉丝点击