100pts 但 TLE*1 求助
查看原帖
100pts 但 TLE*1 求助
462558
ziyistudy楼主2024/5/20 22:05

有题解也是 SPFA 的,快读也加了,就是 TLE*1。

记录

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll MOD=1e9+7;
void read(int& x)
{
    char c=getchar();
    x=0;
    while(c<'0'||c>'9')c=getchar();
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
}
// head
const int N=100005;
vector<vector<PII>> G(N);
int dis[N],vis[N];
bool SPFA(int now)
{
    vis[now]=1;
    for(auto it:G[now]){
        if(dis[now]+it.se>dis[it.fi]){
            dis[it.fi]=dis[now]+it.se;
            if(vis[it.fi]) return true;
            if(SPFA(it.fi)) return true;
        }
    }
    vis[now]=0;
    return false;
}
signed main() 
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n,k;read(n);read(k);
    int opt,a,b;
    for(int i=0;i<k;i++){
        read(opt);read(a);read(b);
        if(opt==1) {G[a].pb({b,0});G[b].pb({a,0});}
        else if(opt==2) G[a].pb({b,1});
        else if(opt==3) G[b].pb({a,0});
        else if(opt==4) G[b].pb({a,1});
        else if(opt==5) G[a].pb({b,0}); 
    }
    for(int i=1;i<=n;i++) G[0].pb({i,0});
    dis[0]=1;
    if(SPFA(0)) printf("-1\n");
    else {
        int minn=1e18,ans=0;
        for(int i=1;i<=n;i++) ans+=dis[i];
        printf("%lld\n",ans);
    }
}
2024/5/20 22:05
加载中...