One example teaches you how to be brave with the subject, and the example is brave
I had A question before. I got stuck with 10 greedy items, but I was still suffering from 11th greedy items,
Won't a question be answered? Greedy. How can we be greedy!
At present, the data of NOIP questions is not clearly defined. It is simple. It is greedy for data!
-By greedy god CCL
Today I made a very bt question.
P3385 [TEMPLATE] Negative Ring
This is actually a wildcard search SPFA. As a wildcard search SPFA, I am not convinced: triumph:
Whoever gets stuck with me will be greedy! : Confused:
Try First
When judging the negative ring, there is a clear conclusion that when you go back to the start point after a wave occurs from the start point and find that the update can continue, there must be a negative ring!
I thought that the data it provided had multiple Unicom components, but it turned out that I thought too much. : Sweat_smile:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>using namespace std;const int MAXN=1e6+10;inline int read(){ char c=getchar();int f=1,x=0; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9')x=x*10+c-48,c=getchar();return x*f;}struct node{ int u,v,w,nxt;}edge[MAXN];int head[MAXN];int num=1;inline void add_edge(int x,int y,int z){ edge[num].u=x; edge[num].v=y; edge[num].w=z; edge[num].nxt=head[x]; head[x]=num++;}int inger[MAXN];int vis[MAXN];int dis[MAXN];inline void pre(){ memset(head,-1,sizeof(head)); num=1; memset(vis,0,sizeof(vis)); memset(dis,0x7f,sizeof(dis));}int n,m;inline void SPFA(){ queue<int>q; q.push(1); vis[1]=1;dis[1]=0; while(q.size()!=0) { int p=q.front(); q.pop(); vis[p]=0; for(int i=head[p];i!=-1;i=edge[i].nxt) { if(dis[edge[i].v]>dis[edge[i].u]+edge[i].w) { dis[edge[i].v]=dis[edge[i].u]+edge[i].w; if(vis[edge[i].v]==0) { vis[edge[i].v]=1; if(edge[i].v==1) { printf("YE5\n"); return ; } q.push(edge[i].v); } } } } printf("N0\n");}int main(){ int T=read(); while(T--) { pre(); n=read();m=read(); for(int i=1;i<=m;i++) { int x=read(),y=read(),z=read(); if(z<0)add_edge(x,y,z); else add_edge(x,y,z),add_edge(y,x,z); } SPFA(); } return 0;}
In this way, A can be used.
However, this seems to be a reference to the concept of dfs,
It gets stuck in the number of queues,
Under normal circumstances, a negative loop is generated only when the number of entries in a node exceeds n, but this is too slow,
We want to narrow down the n
Try Second
N is too big. What should I do?
Let's just random one: grin:
However .. : Dizzy_face:
Try Third
It seems that rand is not working.
Let's specify it.
Like this:
if(inger[edge[i].v]>=250){ printf("YE5\n"); return ;}
250
200
100
.... : Angry:
20
: Grinning:
Think about it,
SPFA time complexity: $ O (ke) $
$ K $ indicates the average number of teams, and $ e $ indicates the number of sides. The value of $ k $ is generally about 2. Therefore, if the number of times a node is added to the queue is greater than 20, a negative ring will appear.
Of course, if you go to the test room, you should write the dfs SPFA. After all, there will be only one chance at that time.