题面
https://www.luogu.org/problem/P1993
题解
那一天好像收到了我女神的$qq$信息呢,女神鼓励我辽~~~
#include<iostream> #include<cstdio> #include<vector> #include<cstdlib> #include<map> using namespace std; int n,m,dis[10500],vis[10500]; vector <int> to[10500],l[10500]; map<int,int> oto[10500]; void add(int u,int v,int c) { if (oto[u].count(v)) l[u][oto[u][v]]=min(c,l[u][oto[u][v]]); else { to[u].push_back(v); l[u].push_back(c); oto[u][v]=to[u].size()-1; } } void dfs(int x,int col){ vis[x]=col; int i,siz=to[x].size(); for (i=0;i<siz;i++) { if (vis[to[x][i]]==0) { dis[to[x][i]]=dis[x]+l[x][i]; dfs(to[x][i],col); } else if (vis[to[x][i]]!=col) continue; else { if (dis[x]+l[x][i]<0) { puts("No"); exit(0); } } } } int main(){ int i,opt,a,b,c; scanf("%d %d",&n,&m); for (i=1;i<=m;i++) { scanf("%d",&opt); if (opt==1) { scanf("%d %d %d",&a,&b,&c); add(a,b,-c); //to[a].push_back(b); //l[a].push_back(-c); } else if (opt==2) { scanf("%d %d %d",&a,&b,&c); add(b,a,c); //to[b].push_back(a); //l[b].push_back(c); } else { scanf("%d %d",&a,&b); add(a,b,0); add(b,a,0); //to[a].push_back(b); //l[a].push_back(0); //to[b].push_back(a); //l[b].push_back(0); } } for (i=1;i<=n;i++) if (vis[i]==0) { dis[i]=0; dfs(i,i); } puts("Yes"); }