洛谷负环板子题

洛谷负环板子题

差点没笑死我

之前的题解都在清一色diss bfs,吹爆dfs

如今改了数据bfs又崛起了,dfs回家种地了,哈哈哈哈哈

dfs版

  1 // luogu-judger-enable-o2
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<queue>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<ctime>
  8 #include<set>
  9 #include<map>
 10 #include<stack>
 11 #include<cstring>
 12 #define inf 2147483647
 13 #define For(i,a,b) for(register long long i=a;i<=b;i++)
 14 #define p(a) putchar(a)
 15 #define g() getchar()
 16 
 17 using namespace std;
 18 long long T;
 19 long long x,y,v;
 20 long long n,m;
 21 bool flag;
 22 bool vis[2010];
 23 long long d[2010];
 24 struct node
 25 {
 26     long long n;
 27     long long v;
 28     node *next;
 29 }*e[400010];
 30 void in(long long &x)
 31 {
 32     long long y=1;
 33     char c=g();x=0;
 34     while(c<'0'||c>'9')
 35     {
 36     if(c=='-')
 37     y=-1;
 38     c=g();
 39     }
 40     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
 41     x*=y;
 42 }
 43 void o(long long x)
 44 {
 45     if(x<0)
 46     {
 47         p('-');
 48         x=-x;
 49     }
 50     if(x>9)o(x/10);
 51     p(x%10+'0');
 52 }
 53 
 54 inline void push(long long x,long long y,long long v)
 55 {
 56     node *p;
 57     p=new node();
 58     p->n=y;
 59     p->v=v;
 60     if(e[x]==NULL)
 61     e[x]=p;
 62     else
 63     {
 64         p->next=e[x]->next;
 65         e[x]->next=p;
 66     }
 67 }
 68 
 69 inline void spfa(long long x)
 70 {
 71     if(flag)return;
 72     if(vis[x]){
 73         flag=true;
 74         return;
 75     }
 76 
 77     vis[x]=true;
 78     for(register node *i=e[x] ;i; i=i->next)
 79     {
 80         if(flag)
 81         return;
 82 
 83         if(d[i->n]>d[x]+i->v){
 84 
 85         d[i->n]=d[x]+i->v;
 86         spfa(i->n);
 87 
 88         }    
 89     }
 90     
 91     vis[x]=false;
 92 }
 93 
 94 int main()
 95 {
 96     in(T);
 97     while(T--)
 98     {
 99         in(n),in(m);
100 
101         For(i,1,n)
102         e[i]=NULL,vis[i]=false,d[i]=inf;    
103         flag=false;
104 
105         For(i,1,m){
106             in(x),in(y),in(v);
107             push(x,y,v);
108             if(v>=0)
109             push(y,x,v);
110         }
111 
112         d[1]=0;
113         spfa(1);
114 
115         if(flag)
116         puts("YE5");
117         else
118         puts("N0");
119     }
120      return 0;
121 }
View Code

bfs版

  1 // luogu-judger-enable-o2
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<queue>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<ctime>
  8 #include<set>
  9 #include<map>
 10 #include<stack>
 11 #include<cstring>
 12 #define inf 2147483647
 13 #define For(i,a,b) for(register long long i=a;i<=b;i++)
 14 #define p(a) putchar(a)
 15 #define g() getchar()
 16 
 17 using namespace std;
 18 long long T;
 19 long long x,y,v;
 20 long long n,m;
 21 bool flag;
 22 bool vis[2010];
 23 long long d[2010];
 24 int num[2010];
 25 
 26 struct node
 27 {
 28     long long n;
 29     long long v;
 30     node *next;
 31 }*e[400010];
 32 queue<int>q;
 33 
 34 void in(long long &x)
 35 {
 36     long long y=1;
 37     char c=g();x=0;
 38     while(c<'0'||c>'9')
 39     {
 40     if(c=='-')
 41     y=-1;
 42     c=g();
 43     }
 44     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
 45     x*=y;
 46 }
 47 void o(long long x)
 48 {
 49     if(x<0)
 50     {
 51         p('-');
 52         x=-x;
 53     }
 54     if(x>9)o(x/10);
 55     p(x%10+'0');
 56 }
 57 
 58 inline void push(long long x,long long y,long long v)
 59 {
 60     node *p;
 61     p=new node();
 62     p->n=y;
 63     p->v=v;
 64     if(e[x]==NULL)
 65     e[x]=p;
 66     else
 67     {
 68         p->next=e[x]->next;
 69         e[x]->next=p;
 70     }
 71 }
 72 
 73 inline void spfa(long long x){
 74    q.push(x);
 75    vis[x]=true;
 76    num[1]++;
 77    while(!q.empty()){
 78     int t=q.front();q.pop();
 79     vis[t]=true;
 80     for(node *i=e[t];i;i=i->next){
 81 
 82         if(d[i->n]>d[t]+i->v){
 83             d[i->n]=d[t]+i->v;
 84             num[i->n]++;
 85                 if(num[i->n]>=n){
 86                     flag=true;
 87                     return;
 88                 }
 89             if(!vis[i->n]){
 90                 q.push(i->n);
 91                 vis[i->n]=true;
 92                 
 93             }
 94         }
 95 
 96     }
 97     vis[t]=false;
 98    }
 99 }
100 
101 int main()
102 {
103     in(T);
104     while(T--)
105     {
106         in(n),in(m);
107 
108         For(i,1,n)
109         e[i]=NULL,vis[i]=false,d[i]=inf,num[i]=0;
110         while(!q.empty())
111             q.pop();  
112         flag=false;
113 
114         For(i,1,m){
115             in(x),in(y),in(v);
116             push(x,y,v);
117             if(v>=0)
118             push(y,x,v);
119         }
120 
121         d[1]=0;
122         spfa(1);
123 
124         if(flag)
125         puts("YE5");
126         else
127         puts("N0");
128     }
129      return 0;
130 }
View Code

 

posted @ 2019-02-13 11:41  WeiAR  阅读(150)  评论(0编辑  收藏  举报