POJ 3228 Network stream + two & and check set

Source: Internet
Author: User

Click to open link

Test instructions: There are n towns, the first line is gold and gold, then the second line is the place of gold and the number of can be loaded, below is the M road, ask you to choose the maximum value of the road, so that all gold to the place where gold is loaded

Idea: The maximum value is minimal, do not consider a look is two points, and then think of is a network flow model, very simple, was a few times the road is a two-way, after the changed a off, and then looked at the discussion can also use and search set write, here are two methods are written, first network flow direct two points maximum value, And then meet the conditions of the edge-building model, run one side on the line

#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h > #include <iostream> #include <algorithm> #include <functional>using namespace Std;typedef long    Long ll;const int inf=0x3f3f3f3f;const int maxn=1010;struct edge{int to,cap,rev; Edge (int a,int b,int c) {to=a;cap=b;rev=c;}}; Vector<edge>g[maxn];int level[maxn],iter[maxn];void add_edge (int from,int to,int cap) {G[from].push_back (Edge (    To,cap,g[to].size ())); G[to].push_back (Edge (From,0,g[from].size ()-1));}    void BFs (int s) {memset (level,-1,sizeof (level));    queue<int>que;level[s]=0;    Que.push (s);        while (!que.empty ()) {int V=que.front (); Que.pop ();            for (unsigned int i=0;i<g[v].size (); i++) {Edge &e=G[v][i];                if (e.cap>0&&level[e.to]<0) {level[e.to]=level[v]+1;            Que.push (e.to); }}}}int dfs (int v,int t,int f) {if (v==t) return F;    for (int &i=iter[v];i<g[v].size (); i++) {Edge &e=G[v][i];            if (e.cap>0&&level[v]<level[e.to]) {int D=dfs (e.to,t,min (F,e.cap));                if (d>0) {e.cap-=d;                G[e.to][e.rev].cap+=d;            return D; }}} return 0;}    int max_flow (int s,int t) {int flow=0;        while (1) {BFS (s);        if (level[t]<0) return flow;        memset (Iter,0,sizeof (ITER));        int F;    while ((F=dfs (S,t,inf)) >0) flow+=f; }}int a[maxn*100],b[maxn*100],u[maxn*100],v[maxn*100],cost[maxn*100],n,m,sum;int judge (int mid) {for (int i=0;i<    maxn;i++) g[i].clear ();    for (int i=1;i<=n;i++) Add_edge (0,i,a[i]);    for (int i=1;i<=n;i++) Add_edge (I,n+1,b[i]);            for (int i=1;i<=m;i++) {if (Cost[i]<=mid) {Add_edge (u[i],v[i],inf);        Add_edge (V[i],u[i],inf);    }} int Ans=max_flow (0,n+1);    if (ans==sum) return 1; else return 0;} int main () {   while (scanf ("%d", &n)!=-1) {if (n==0) break;        sum=0;            for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);        Sum+=a[i];        } for (int i=1;i<=n;i++) scanf ("%d", &b[i]);        scanf ("%d", &m);        for (int i=1;i<=m;i++) scanf ("%d%d%d", &u[i],&v[i],&cost[i]);        int le=0,ri=10010;            while (ri-le>1) {int mid= (LE+RI) >>1;            if (judge (mid)) Ri=mid;        else Le=mid;        } if (ri==10010) printf ("No solution\n");    else printf ("%d\n", RI); } return 0;}
The second kind is and check set, did not expect to use and check set to write, read discuss everyone said so much faster, wrote a pitch, want is the largest side of the smallest, you can use the greedy thought first sort, from small to large, meet the conditions of the current added to the edge of the largest must be the smallest, then how to judge it, Open two weights array, one record is the number of gold deposits developed in the collection, and the other is the amount of gold that is available in the collection, when the amount of gold in the collection equals the total gold amount and the amount of gold >= the total gold amount is satisfied, before WA a few is because m can be equal to 0, So if

4

2 3 0 0

3 3 0 0

0

The wrong output is No solution, but should output 0

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < Algorithm>using namespace Std;typedef Long long ll;typedef unsigned long long ull;const int Inf=0x3f3f3f3f;const ll INF =0x3f3f3f3f3f3f3f3fll;const int Maxn=10010;int f[maxn],num_gol[maxn],num_cav[maxn],a[maxn],b[maxn],n,m,sum;struct edge{int u,v,cost;} Es[maxn*100];bool cmp (const edge &a,const edge &b) {return a.cost<b.cost;}    int find1 (int x) {if (x!=f[x]) f[x]=find1 (f[x]); return f[x];}    void Unite (int a,int b) {int aa=find1 (a);    int Bb=find1 (b);    if (AA==BB) return;    F[AA]=BB;    NUM_GOL[BB]+=NUM_GOL[AA]; NUM_CAV[BB]+=NUM_CAV[AA];}    int Slove () {int cnt=0,flag=0,ans=0;            for (int i=0;i<m;i++) {if (Find1 (Es[i].u!=find1 (ES[I].V))) {unite (ES[I].U,ES[I].V);            int Tt=find1 (ES[I].U);            Ans=max (Ans,es[i].cost);         if (num_gol[tt]==sum&&num_cav[tt]>=sum) {flag=1;break;   }} if (flag) break;    } if (flag) return ans; else return-1;}        int main () {while (scanf ("%d", &n)!=-1) {if (n==0) break;        sum=0;        for (int i=0;i<=n;i++) f[i]=i;        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);        for (int i=1;i<=n;i++) scanf ("%d", &b[i]);            for (int i=1;i<=n;i++) {int tt=a[i]-b[i];                if (tt>=0) {num_gol[i]=a[i]-b[i];                num_cav[i]=0;            Sum+=num_gol[i];                }else{num_gol[i]=0;            Num_cav[i]=b[i]-a[i];        }} scanf ("%d", &m);        for (int i=0;i<m;i++) scanf ("%d%d%d", &es[i].u,&es[i].v,&es[i].cost);        Sort (es,es+m,cmp);        if (sum==0) {printf ("0\n"); continue;        } int ans=slove ();        if (ans==-1) printf ("No solution\n");    else printf ("%d\n", ans); } return 0;}

POJ 3228 Network stream + two & and check set

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.