Luo Gu P3627 [APIO2009] looting plan problem solution

Analysis

FIG built + + strongly connected components longest path seeking the SPFA

But to ensure that the final arrival point is included in pub

Although the idea is not hard to do, but demanding code capability.

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<queue>
  6 #define maxn 500010
  7 using namespace std;
  8 inline int read() 
  9 {
 10     int x=0;
 11     bool f=1;
 12     char c=getchar();
 13     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
 14     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
 15     if(f) return x;
 16     return 0-x;
 17 }
 18 inline void write(int x)
 19 {
 20     if(x<0){putchar('-');x=-x;}
 21     if(x>9)write(x/10);
 22     putchar(x%10+'0');
 23 }
 24 struct node
 25 {
 26     int from,to,val,nex;
 27 }edge[2*maxn];
 28 int n,m,s,p,cnt,ans,top,inl,num;
 29 int x[maxn],y[maxn],head[maxn],dfn[maxn],low[maxn],st[maxn],inn[maxn],sum[maxn],si[maxn],money[maxn],dis[maxn];
 30 bool book[maxn],book1[maxn],bar[maxn];
 31 inline void clear()
 32 {
 33     cnt=0;
 34     memset(edge,0,sizeof(edge));
 35     memset(head,0,sizeof(head));
 36 }
 37 inline void add(int x,int y)
 38 {
 39     cnt++;
 40     edge[cnt].to=y;
 41     edge[cnt].nex=head[x];
 42     head[x]=cnt;
 43 }
 44 inline void build(int x,int y,int z)
 45 {
 46     cnt++;
 47     edge[cnt].from=x;
 48     edge[cnt].to=y;
 49     edge[cnt].val=z;
 50     edge[cnt].nex=head[x];
 51     head[x]=cnt;
 52 }
 53 inline void Tarjan(int u)
 54 {
 55     dfn[u]=low[u]=++num;
 56     st[++top]=u;
 57     for(int i=head[u];i;i=edge[i].nex)
 58     {
 59         int v=edge[i].to;
 60         if(!dfn[v])
 61         {
 62             Tarjan(v);
 63             low[u]=min(low[u],low[v]);
 64         }
 65         else if(!inn[v])
 66             low[u]=min(low[u],dfn[v]);
 67     }
 68     if(low[u]==dfn[u])
 69     {
 70         inn[u]=++inl;
 71         while(st[top]!=u)
 72         {
 73             if(bar[st[top]])book1[inl]=1;
 74             sum[inl]+=money[st[top]];
 75             inn[st[top]]=inl;
 76              --top;
 77         }
 78         if(bar[st[top]])book1[inl]=1;
 79         sum[inl]+=money[st[top]];
 80         --top;
 81     }
 82 }
 83 inline void spfa()
 84 {
 85     memset(book,0,sizeof(book));
 86     for(int i=1;i<=n;i++) dis[i]=0;
 87     queue<int> q;
 88     q.push(inn[s]);
 89     book[inn[s]]=1;
 90     dis[inn[s]]=sum[inn[s]];
 91     while(!q.empty())
 92     {
 93         for(int i=head[q.front()];i;i=edge[i].nex)
 94         {
 95             if(dis[edge[i].to]<dis[edge[i].from]+edge[i].val)
 96             {
 97                 dis[edge[i].to]=dis[edge[i].from]+edge[i].val;
 98                 if(book[edge[i].to]==0)
 99                 {
100                     q.push(edge[i].to);
101                     book[edge[i].to]=1;
102                 }
103             }
104         }
105         book[q.front()]=0;
106         q.pop();
107     }
108 }
109 int main()
110 {
111     n=read();m=read();
112     for(int i=1;i<=m;i++)
113     {
114         x[i]=read();y[i]=read();
115         add(x[i],y[i]);
116     }
117     for(int i=1;i<=n;i++)money[i]=read();
118     s=read();p=read();
119     
120     for(int i=1;i<=p;i++)
121     {
122         int x;
123         x=read();
124         bar[x]=1;
125     }
126     for(int i=1;i<=n;i++)
127         if(!dfn[i])
128             Tarjan(i);
129     
130     clear();
131     for(int i=1;i<=m;i++)
132         if(inn[x[i]]!=inn[y[i]])
133             build(inn[x[i]],inn[y[i]],sum[inn[y[i]]]);
134     spfa();
135     for(int i=1;i<=inl;i++)
136         if(book1[i]==1)
137             ans=max(ans,dis[i]);
138     write(ans);
139     return 0;
140 }
Please Gangster treatise(Anyway, I do not know what that means treatise)

 

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11250401.html