Luo Gu original page
solution:
This is a differential constraint problem is not constrained differential system of human way, please
differential restraint system in the form of a triangle inequality much like single-source shortest path, it has been listed in graph theory algorithms to equation (I was seeking at least by worth seeking algorithm, find the maximum contrast)
\[ \left\{\begin{matrix} & a-b \geq c(k=1)& \\ & b-a \geq c (k=2)& \\ & a-b \geq 0, b-a \geq 0(k=3) & \end{matrix}\right.\]
If the construction of FIG negative ring (longest run is seeking ring road), i.e., small KSmokedmisremembered.
I had to fight this problem is based on BFS_SPFA, then overtime,SPFA has died, feelI changed dishesAlthough DFS_SPFA easy to use but still exponential complexity,In particular there is likely to be the topic of people jammed cancerTLE is easy, because it is subject data range \ (n, m \ leq 10000 \) I can be divided into two in the embodiment, if \ (n-m * \. 7 GEQ 1E + \) , then hung DFS_SPFA, if not meet, selectKe learning methodBFS_SPFA,
BFS_SPFA find negative ring
An array as referred to \ (CNT [I] \) , represents the number of sides to, if \ (CNT [I] \ GEQ m \) , i.e., negative loop, high efficiency of this methodBut the worst is O (nm) or deadThan many online sacrifice correctness to run more harm than good and sometimes better algorithms.
CodeI know that you like the most:
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#define re register
using namespace std;
template<typename T>
inline void read(T&x)
{
x=0;
char s=getchar();
bool f=false;
while(!(s>='0'&&s<='9'))
{
if(s=='-')
f=true;
s=getchar();
}
while(s>='0'&&s<='9')
{
x=(x<<1)+(x<<3)+s-'0';
s=getchar();
}
if(f)
x=(~x)+1;
}
const int N=1000010;
int dis[N];
struct Edge
{
int next,to,dis;
} edge[N];
int num_edge,head[N],n,m,cnt[N],*v,*w;
bool exist[N];
inline void add_edge(const int&from,const int&to,const int&dis)
{
edge[++num_edge].next=head[from];
head[from]=num_edge;
edge[num_edge].to=to;
edge[num_edge].dis=dis;
}
inline void dfs_spfa(int u)
{
exist[u]=true;
for(re int i=head[u]; i; i=edge[i].next)
{
v=&edge[i].to;
w=&edge[i].dis;
if(dis[*v]<dis[u]+(*w))
{
dis[*v]=dis[u]+(*w);
if(exist[*v])
{
printf("No\n");
exit(0);
}
else
dfs_spfa(*v);
}
}
exist[u]=false;
}
queue<int>q;
inline void bfs_spfa()
{
dis[0]=0;
q.push(0);
re int u;
do
{
u=q.front();
q.pop();
exist[u]=false;
for(re int i=head[u]; i; i=edge[i].next)
{
v=&edge[i].to;
w=&edge[i].dis;
if(dis[*v]<dis[u]+(*w))
{
dis[*v]=dis[u]+(*w);
cnt[*v]=cnt[u]+1;
if(cnt[*v]>m)
{
printf("No\n");
return;
}
if(!exist[*v])
{
exist[*v]=true;
q.push(*v);
}
}
}
}
while(!q.empty());
printf("Yes\n");
}
int main()
{
read(n);
for(re int i=1; i<=n; i++)
dis[i]=-0x3f3f3f3f;
read(m);
for(re int i=1,k,a,b,c; i<=m; i++)
{
read(k);
read(a);
read(b);
if(k==1)
{
read(c);
add_edge(b,a,c);
}
else if(k==2)
{
read(c);
add_edge(a,b,-c);
}
else if(k==3)
{
add_edge(a,b,0);
add_edge(b,a,0);
}
}
for(re int i=1; i<=n; i++)
add_edge(0,i,0);
if(n*m>=1e7)
{
dfs_spfa(0);
printf("Yes\n");
}
else
bfs_spfa();
return 0;
}
If the differential restraint system has been a great foundation, it is recommended to do
[SCOI2011] candy
and cashier issues