MZOJ #56 国王

分析

差分约束板子题,用前缀和建边,跑一遍最长路就好

注意初始化

代码

  1 /**********************
  2 User:Mandy.H.Y
  3 Langauge:c++
  4 Problem:
  5 Algorithm:
  6 **********************/
  7 
  8 #include<bits/stdc++.h>
  9 
 10 using namespace std;
 11 
 12 const int maxn = 105;
 13 const int maxm = 105;
 14 const int mod = 200;
 15 
 16 int n,m,first[maxn],size;
 17 int q[maxn << 1],l,r,cnt[maxn];
 18 bool vis[maxn];
 19 int dis[maxn];
 20 
 21 struct Edge{
 22     int v,w,nt;
 23 }edge[maxm << 1];
 24 
 25 template<class T>inline void read(T&x){
 26     x = 0;bool flag = 0;char ch = getchar();
 27     while(!isdigit(ch)) flag |= ch == '-',ch = getchar();
 28     while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
 29     if(flag) x = -x;
 30 }
 31 
 32 template<class T>void putch(const T x){
 33     if(x > 9) putch(x / 10);
 34     putchar(x % 10 | 48);
 35 }
 36 
 37 template<class T>void put(const T x){
 38     if(x < 0) putchar('-'),putch(-x);
 39     else putch(x);
 40 }
 41 
 42 void eadd(int u,int v,int w){
 43     edge[++size].v = v;
 44     edge[size].w = w;
 45     edge[size].nt = first[u];
 46     first[u] = size;
 47 }
 48 
 49 void readdata(){
 50     for(int i = 1;i <= m; ++ i){
 51         int s,nn,k;
 52         char c[5];
 53         read(s);read(nn);
 54         nn = s + nn;--s;
 55         scanf("%s",c);
 56         read(k);
 57         if(c[0] == 'g'){
 58             eadd(s,nn,k+1);
 59         }else {
 60             eadd(nn,s,-k+1);
 61         }
 62     }
 63 }
 64 
 65 void init(){
 66     size = 0;//注意初始化-- 
 67     memset(vis,0,sizeof(vis));
 68     memset(cnt,0,sizeof(cnt));
 69     memset(dis,0,sizeof(dis));
 70     memset(first,0,sizeof(first));
 71     memset(edge,0,sizeof(edge));
 72 }
 73 
 74 void work(){
 75     init();
 76     readdata();
 77     l = r = 0;//注意这里的初始化
 78     //因为下面没有mod 
 79     
 80     for(int i = 0;i <= n; ++ i){
 81         q[r++] = i;
 82         vis[i] = 1;
 83     }
 84     
 85     while(l != r){
 86         int u = q[l];
 87         l++;
 88         l %= mod;
 89         vis[u] = 0;
 90         if(cnt[u] > n) {
 91             puts("successful conspiracy");
 92             return;
 93         }
 94         int rr = (r-1+mod)%mod;
 95         if(l != r) if(dis[q[l]] < dis[q[rr]]) swap(q[l],q[rr]);
 96         for(int i = first[u];i;i = edge[i].nt){
 97             int v = edge[i].v;
 98             int w = edge[i].w;
 99             if(dis[v] < dis[u] + w){
100                 dis[v] = dis[u] + w;
101                 cnt[v] = cnt[u] + 1;
102                 if(!vis[v]){
103                     vis[v] = 1;
104                     q[r++] = v;
105                     r %= mod;
106                     int rr = (r-1+mod)%mod;
107                     if(l != r) if(dis[q[l]] < dis[q[rr]]) swap(q[l],q[rr]);
108                 }
109             }
110         }
111     }
112     puts("lamentable kingdom");
113 }
114 
115 int main(){
116 //    freopen("1.txt","r",stdin);
117     while(scanf("%d",&n)!=EOF && n){
118         read(m);
119         work();
120     }
121     return 0;
122 }
View Code

 

posted @ 2019-09-05 19:12  Mandy_H_Y  阅读(151)  评论(0编辑  收藏  举报