dfs链式前项星TLE 55
查看原帖
dfs链式前项星TLE 55
1055198
Charlie_ljk楼主2024/1/28 07:08
#include<bits/stdc++.h>
#define int long long 
#define endl '\n'
using namespace std;
const int N=1e6+10,P=1e9+7;
template<typename Tp> inline void read(Tp&x)
{
    x=0;register bool z=1;
    register char c=getchar();
    for(;c<'0'||c>'9';c=getchar()) if(c=='-') z=0;
    for(;'0'<=c&&c<='9';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
    x=(z?x:~x+1);
}
int n,m,d[N];
int head[N],nxt[N],to[N],w[N],tot;
bool v[N];
void add(int x,int y,int z)
{
    nxt[++tot]=head[x];
    to[tot]=y;
    w[tot]=z;
    head[x]=tot;
}
bool spfa(int x)
{
    v[x]=1;
    for(int i=head[x];i!=-1;i=nxt[i])
    {
        int y=to[i],z=w[i];
        if(d[y]>d[x]+z)
        {
            d[y]=d[x]+z;
            if(v[y]||!spfa(y)) 
                return 0;
        }
    }
    v[x]=0;
    return 1;
}
signed main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    read(n),read(m);
    int s,x,y,z;
    for(int i=0;i<=n;i++) head[i]=-1;
    for(int i=1;i<=m;i++)
    {
        read(s);
        if(s==1)
            read(x),read(y),read(z),
            add(x,y,~z+1);
        else if(s==2)
            read(x),read(y),read(z),
            add(y,x,z);
        else 
            read(x),read(y),
            add(x,y,0),add(y,x,0);
    }
    for(int i=1;i<=n;i++) add(0,i,0);
    memset(d,0x3f,sizeof(d));
    d[0]=0;
    cout<<(spfa(0)?"Yes":"No");
}
2024/1/28 07:08
加载中...