标签:ring 整数 表示 def get 必须 getch else 数字
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 100001
#define maxm 100001
using namespace std;
int n,m;
long long ans;
inline int read(){
    register int x(0),f(1); register char c(getchar());
    while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
    while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
struct edge{
    int to,dis,next;
    edge(){}
    edge(const int &_to,const int &_dis,const int &_next){ to=_to,dis=_dis,next=_next; }
}e[maxm<<2];
int head[maxn],k;
inline void add(const int &u,const int &v,const int &w){ e[k]=edge(v,w,head[u]),head[u]=k++; }
long long dis[maxn];
int cnt[maxn];
bool inq[maxn];
queue<int> q;
inline bool spfa(){
    q.push(0),inq[0]=true;
    while(q.size()){
        int u=q.front(); inq[u]=false,q.pop();
        for(register int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(dis[v]<dis[u]+e[i].dis){
                dis[v]=dis[u]+e[i].dis,cnt[v]=cnt[u]+1;
                if(cnt[v]==n+1) return false;
                if(!inq[v]) q.push(v),inq[v]=true;
            }
        }
    }
    return true;
}
int main(){
    memset(head,-1,sizeof head);
    n=read(),m=read();
    for(register int i=1;i<=m;i++){
        int op=read(),u=read(),v=read();
        if(op==1) add(u,v,0),add(v,u,0);
        if(op==2){
            if(u==v){ puts("-1"); return 0; }
            add(u,v,1);
        }
        if(op==3) add(v,u,0);
        if(op==4){
            if(u==v){ puts("-1"); return 0; }
            add(v,u,1);
        }
        if(op==5) add(u,v,0);
    }
    for(register int i=n;i>=1;i--) add(0,i,1);
    if(!spfa()) puts("-1");
    else{
        for(register int i=1;i<=n;i++) ans+=dis[i];
        printf("%lld\n",ans);
    }
    return 0;
}标签:ring 整数 表示 def get 必须 getch else 数字
原文地址:https://www.cnblogs.com/akura/p/11066491.html