Usaco/the First Wave

Source: Internet
Author: User

bzoj1572: Greedy. First sort by chronological order, and then use the priority queue, if the time does not contradict directly inserted, otherwise determine the queue w smallest element is replaced. (I didn't use llwa once.

#include <cstdio> #include <cstring> #include <iostream> #include <queue> #include < algorithm>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i-- ) #define LL Long long#define CLR (x,c) memset (x,c,sizeof (x)) int read () {int X=0;char c=getchar (); while (!isdigit (c)) c= GetChar (); while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int Nmax=100005;struct node{int num,w;bool operator< (const NODE&AMP;RHS) const{return num<rhs.num;}}; Node nodes[nmax];struct node{int num,w;bool operator< (const NODE&AMP;RHS) const{return w>rhs.w;}}; Node Nodes[nmax];p riority_queue<node>q;int main () {int n=read (); REP (I,1,n) Nodes[i].num=read (), Nodes[i].w=read (); sort (nodes+1,nodes+n+1); REP (i,1,n) NODES[I].NUM=NODES[I].NUM,NODES[I].W=NODES[I].W; REP (i,1,n) {node o=nodes[i];if (o.num>q.size ()) {Q.push (o); continue;} Node Tp=q.top (); if (TP.W&LT;O.W) Q.pop (), Q.push (o);} ll Ans=0;while (!q.empty ()) Ans+=q.top (). W,q.pop ();p rintf ("%Lld\n ", ans); return 0;} 

bzoj1574: Greedy, remove the unreachable points from the connected point set, and then Dfs. (two array names are the same as re once

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) #define CLR (x,c) memset (X,c, sizeof (x)) #define QWQ (x) for (Edge *o=head[x];o;o=o->next) #define OP () CLR (head,0);p T=edges;int read () {int X=0;char C=getchar (); while (!isdigit (c)) C=getchar () and while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int Nmax=30005;const int maxn=200005;struct edge{int to;edge *next;}; Edge Edges[maxn],*pt,*head[nmax];bool vis[nmax],v[nmax];void adde (int u,int V) {Pt->to=v;pt->next=head[u];head [u]=pt++;p t->to=u;pt->next=head[v];head[v]=pt++;} void Dfs (int x) {V[X]=TRUE;QWQ (x) if (vis[o->to]&&! V[o->to]) DFS (o->to);} int main () {op (); CLR (vis,true); CLR (v,false); int n=read (), M=read (), P=read (), u,v; REP (i,1,m) U=read (), V=read (), Adde (U,V);    REP (i,1,p) {u=read (); Vis[u]=false;qwq (U) vis[o->to]=false; }dfs (1); int ans=n; REP (I,1,n) if (V[i]) ans--;p rintf ("%d\n", ans); return 0;} 

bzoj1576: Because the shortest path is unique. So there is the shortest path tree. Edges that are not in the tree are u,v for points below the LCA (U,V), with Dist[x]=dist[u]+dist[v]+val[u,v]-dist[x] (unrelated to dist[x). You can sort dist[u]+dist[v]+val[u,v] and update it sequentially. Because the first update must be better, so you can use and check the set of compressed path.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < queue>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) # Define CLR (x,c) memset (x,c,sizeof (x)) #define QWQ (x) for (edge *o=head[x];o;o=o->next) int read () {int X=0;char c= GetChar (); while (!isdigit (c)) C=getchar () and while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int NMAX=100005;CONST int Maxn=400005;const int inf=0x7f7f7f7f;struct edge{int to,dist;edge *next;}; Edge Edges[maxn],*pt,*head[nmax];int d[nmax],dep[nmax],fa[nmax],ans[nmax];void adde (int u,int v,int D) {pt->to=v; pt->dist=d;pt->next=head[u];head[u]=pt++;p t->to=u;pt->dist=d;pt->next=head[v];head[v]=pt++;} struct Node{int x,d;node (int x,int D): X (x), D (d) {}bool operator< (const NODE&AMP;RHS) const{return d>rhs.d;}}; Priority_queue<node>q;void Dijkstra () {clr (d,0x7f);d [1]=0;dep[1]=0;q.push (Node (1,0)), while (!q.empty ()) { Node Tmp=q.top (); Q.Pop (); if (D[TMP.X]!=TMP.D) continue;qwq (tmp.x) if (d[o->to]>d[tmp.x]+o->dist) {d[o->to]=d[tmp.x]+o-> Dist;dep[o->to]=dep[tmp.x]+1;fa[o->to]=tmp.x;q.push (Node (o->to,d[o->to));}}} struct Zc{int from,to,dist;bool operator< (const ZC&AMP;RHS) const{return dist<rhs.dist;}}; ZC zcs[maxn];void GETDEP (int x) {}int update (int u,int v,int val) {if (u==v) return u;if (Dep[u]<dep[v]) swap (U,V); if (ans [U]==-1] Ans[u]=val-d[u];return fa[u]=update (fa[u],v,val);} int main () {clr (head,0);p t=edges;int n=read (), M=read (), U,V,DD; REP (i,1,m) {u=read (), V=read (), Dd=read (), Adde (U,V,DD); Zc &oo=zcs[i];oo.from=u,oo.to=v,oo.dist=dd;} Dijkstra (); REP (i,1,n) printf ("%d:%d\n", I,dep[i]); int cnt=0; REP (i,1,m) {Zc &oo=zcs[i];if (d[oo.from]==d[oo.to]+oo.dist| | d[oo.to]==d[oo.from]+oo.dist) Continue;zc &ee=zcs[++cnt];ee.from=oo.from,ee.to=oo.to,ee.dist=d[oo.from]+d[ Oo.to]+oo.dist;} Sort (zcs+1,zcs+cnt+1); CLR (ans,-1); REP (i,1,cnt) update (zcs[i].from,zcs[i].to,zcs[i].dist); REP (i,2,n) printf ("%d\n", Ans[i]); return 0;} 

bzoj1585: The minimum cut is possible after the map is built. (Because the first point is not processed, WA once

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define REP (i,s,t) for (int i=s;i<=t;i++) #define CLR (x,c) memset (x,c,sizeof (x)) int read () {int X=0;char C=getchar (); while (!isdigit (c)) C=getchar () and while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int NMAX=10005;CONST int Maxn=200005;const int inf=0x7f7f7f7f;struct edge{int to,cap;edge *next,*rev;}; Edge edges[maxn],*pt,*head[nmax],*cur[nmax],*p[nmax];void Add (int u,int v,int D) {pt->to=v;pt->cap=d;pt-> next=head[u];head[u]=pt++;} void Adde (int u,int v,int d) {Add (u,v,d); add (v,u,0); head[u]->rev=head[v];head[v]->rev=head[u];} int cnt[nmax],h[nmax];int maxflow (int s,int t,int n) {clr (cnt,0); cnt[0]=n;clr (h,0); int Flow=0,a=inf,x=s;edge *e;while ( H[s]<n) {for (E=cur[x];e;e=e->next) if (e->cap>0&&h[x]==h[e->to]+1) break;if (e) {a=min (a,e- &GT;CAP); cur[x]=p[e->to]=e;x=e->to;if (x==t) {while (x!=s) p[x]->cap-=a,p[x]->rev->cap+=a,x=p[x]->rev->to;flow+=a,a=inf;}} Else{if (!--cnt[h[x]]) break;h[x]=n;for (e=head[x];e;e=e->next) if (e->cap>0&&h[x]>h[e->to]+ 1) h[x]=h[e->to]+1,cur[x]=e;cnt[h[x]]++;if (x!=s) x=p[x]->rev->to;}} return flow;} BOOL Vis[nmax];int Main () {clr (head,0);p t=edges;clr (vis,false); int n=read (), M=read (), P=read (), S=0,t=n+n+1;adde (s,1 , INF); REP (i,1,m) {int u=read (), V=read (); Adde (U+u,v+v-1,inf); Adde (V+v,u+u-1,inf);} REP (i,1,p) {int u=read (); vis[u]=true;} Adde (1,2,inf); REP (i,2,n) {if (Vis[i]) Adde (I+i-1,i+i,inf), Adde (I+i,t,inf), Else Adde (i+i-1,i+i,1);} printf ("%d\n", Maxflow (s,t,t+1)); return 0;}

Bzoj1589:tarjan after shrinking points. (Read-in optimization x=x*1+c-' 0 ', because it's all small data, so I didn't find WA once.)

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < stack>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) # Define CLR (x,c) memset (x,c,sizeof (x)) #define QWQ (x) for (edge *o=head[x];o;o=o->next) int read () {int X=0;char c= GetChar (); while (!isdigit (c)) C=getchar () and while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int NMAX=200005;CONST int Maxn=400005;const int inf=0x7f7f7f7f;struct edge{int to;edge *next;}; Edge edges[maxn],*pt,*head[nmax];void Add (int u,int v) {pt->to=v;pt->next=head[u];head[u]=pt++;} int sccno[nmax],scc[nmax],pre[nmax],scc_cnt,dfs_clock=0;stack<int>s;int dfs (int x) {int Lowu=pre[x]=++dfs_ Clock;s.push (x); Qwq (x) {int to=o->to;if (!pre[to]) lowu=min (Lowu,dfs (to)), else if (!sccno[to]) lowu=min (lowu,pre[ To]);} if (Lowu==pre[x]) {scc_cnt++;scc[scc_cnt]=0;while (1) {int tmp=s.top (); S.pop (); sccno[tmp]=scc_cnt;scc[scc_cnt]++;if (tmp==x) break;}} Return Lowu;} int sum[nmax];int getsum (int x) {if (sum[x]) return sum[x];int ans=scc[x];qwq (x) ans+=getsum (o->to); return ans;} int main () {clr (head,0);p t=edges;int n=read (), u; REP (I,1,n) U=read (), add (i,u); scc_cnt=n; REP (I,1,n) if (!pre[i]) DFS (i); REP (i,1,n) qwq (i) if (Sccno[i]!=sccno[o->to]) Add (sccno[i],sccno[o->to]); REP (i,n+1,scc_cnt) sum[i]=getsum (i); REP (i,1,n) printf ("%d\n", Sum[sccno[i]]); return 0; }

Summary

1. A variety of magical wrong ways ...

2. Do not make your own data too small ... But do not be yourself out of the data around the halo ...

Usaco/the First Wave

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.