【codevs 1961】躲避大龍

spfa

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=70000+5;
int n,m;
struct edge
{
    int f,t,v;
}es[maxn<<1];
struct hehe
{
    int p,v;
};
queue<hehe>q;
int  vis[maxn][61];
int first[maxn],nxt[maxn<<1],tot=0;
void build(int f,int t,int v)
{
    es[++tot]=(edge){f,t,v};
    nxt[tot]=first[f];
    first[f]=tot;
}
void spfa()
{
    while(!q.empty())
    {
        int p=q.front().p,v=q.front().v;
        q.pop();
        for(int i=first[p];i;i=nxt[i])
        {
            int at=es[i].t,av=(v+es[i].v)%60;
            {
                while(av<0) av+=60;
                if(!vis[at][av])
                {
                    q.push((hehe){at,av});
                    vis[at][av]=1;
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(vis,0,sizeof(vis));
    int a,c,b;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        build(a,b,c);
        build(b,a,c);
    }
    vis[1][0]=1;
    q.push((hehe){1,0});
    spfa();
    int ans;
    for(ans=0;ans<=59;ans++)
    {
        if(vis[2][ans]) break;
    }
    printf("%02d\n",ans);
    return 0;
}
發佈了68 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章