hdu-1874 题解

裸SPFA模板题,注意算法中每次扩展完成后要把vis标记取消

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<climits>
using namespace std;
struct edgetype
{
    int to;
    int len;
};
struct pointtype
{
    bool vis;
    vector<edgetype> next;
    int dis;
}point[201];
queue<int> q;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        for(int i=0;i<n;i++)
        {
            point[i].vis=false;
            point[i].dis=INT_MAX;
            point[i].next.clear();
        }
        for(int i=1;i<=m;i++)
        {
            int s,t,l;
            cin>>s>>t>>l;
            edgetype tmp;
            tmp.to=t;
            tmp.len=l;
            point[s].next.push_back(tmp);
            tmp.to=s;
            point[t].next.push_back(tmp);
        }
        int s,t;
        cin>>s>>t;
        point[s].dis=0;
        point[s].vis=true;
        q.push(s);
        while(!q.empty())
        {
            int now=q.front();
            vector<edgetype>::iterator it;
            for(it=point[now].next.begin();it!=point[now].next.end();it++)
            {
                if(point[(*it).to].dis>point[now].dis+(*it).len)
                {
                    point[(*it).to].dis=point[now].dis+(*it).len;
                    if(!point[(*it).to].vis)
                    {
                        point[(*it).to].vis=true;
                        q.push((*it).to);
                    }
                }
            }
            point[now].vis=false;
            q.pop();
        }
        int res=point[t].dis;
        if(res==INT_MAX)
        {
            res=-1;
        }
        cout<<res<<endl;;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值