跳到正文

个人记录

abcdefg

wuweize150815约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年7月10日
更新
2026年7月10日
篇幅
约 3 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int id;
    long long w;
};
long long dis[100005] , cnt[100005];
bool vis[100005];
vector <node> g[100005];
long long sum=0;
int n , m , v , a , b;
void SPFA ( )
{
    queue <node> q;
    q.push ({0 , 0});
    dis[0]=0;
    vis[0]=1;
    cnt[0]=1;
    while (!(q.empty ( )))
    {
        node p=q.front ( );
        q.pop ( );
        vis[p.id]=0;
		if (cnt[p.id]>n+1)
        {
            cout << -1;
            exit (0);
        }
        for (auto i:g[p.id])
        {
            if (dis[i.id]<dis[p.id]+i.w)
            {
                dis[i.id]=dis[p.id]+i.w;
                cnt[i.id]=cnt[p.id]+1;
                if (vis[i.id]==1) continue;
                vis[i.id]=1;
                q.push ({i.id , dis[i.id]});
            }
        }
    }
}

inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=x*10+ch-48; ch=getchar(); }
	return x*f;
}
int main ( )
{
    n=read ( ) , m=read ( );
    for (int i=1;i<=n;i++)
    {
        g[0].push_back ({i , 1});
    }
    while (m--)
    {
        v=read ( ) , a=read ( ) , b=read ( );
        if (v==1)
        {
            g[a].push_back ({b , 0});
            g[b].push_back ({a , 0});
        }
        if (v==2)
        {
            g[a].push_back ({b , 1});
        }
        if (v==3)
        {
            g[b].push_back ({a , 0});
        }
        if (v==4)
        {
            g[b].push_back ({a , 1});
        }
        if (v==5)
        {
            g[a].push_back ({b , 0});
        }
    }
    SPFA ( );
    for (int i=1;i<=n;i++)
    {
        sum+=dis[i];
    }
    cout << sum;
    return 0;
}
原文