UV 10779 (max Stream)

Source: Internet
Author: User

Question link:Http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? Id = 33631

Theme: Bob has some pasters that he can exchange with others. He can take out his unique pasters, you can also take out repeated pasters (sometimes you can take out unique pasters instead of duplicate pasters to switch to more pasters ).

Bob's friends also have some pasters, but they only exchange their repeated pasters with Bob, and they do not.

Find the maximum number of stickers Bob can have.

Solutions:

The question is very clear. Even if you take out the repeated stickers, it is not necessarily the best. If you are greedy, you don't have to try it.

Global Resource Allocation requires the use of the network flow model.

Graph creation method:

① S (as Bob)-> all items: one edge, and cap is the number of stickers owned by Bob.

②: All friends-> all items: if the number of stickers held by this person is greater than or equal to 2, the CAP indicates the number of stickers-1. (The reason is that these people only take out repeated stickers ).

③: All items-> all friends: if this person has not changed the item, he or she will be connected to an edge. Cap = 1 ,. (Because these people will accept stickers they don't have)

④: All items-> T: an edge is connected, Cap = 1, and the type of items is counted.

 

After the figure is created, all items can be regarded as Bob's total assets. The total assets can flow in or out. On this basis, the result is the maximum flow.

Although the Code uses a pitfall edge table, although it is not as fast as chain forward stars, ISAP + gap optimization + current arc optimization is fast enough.

 

#include "cstdio"#include "vector"#include "cstring"#include "queue"using namespace std;#define maxn 405#define inf 100000000struct Edge{    int from,to,cap,flow;    Edge(int FROM,int TO,int CAP,int FLOW):from(FROM),to(TO),cap(CAP),flow(FLOW) {}};int d[maxn],p[maxn],gap[maxn],cur[maxn],paste[15][30];bool vis[maxn];vector<int> G[maxn];vector<Edge> edges;void addedge(int from,int to,int cap){    edges.push_back(Edge(from,to,cap,0));    edges.push_back(Edge(to,from,0,0));    int m=edges.size();    G[from].push_back(m-2);    G[to].push_back(m-1);}void bfs(int s,int t){    memset(vis,false,sizeof(vis));    memset(d,0,sizeof(d));    memset(p,0,sizeof(p));    d[t]=0;vis[t]=true;    queue<int> Q;Q.push(t);    while(!Q.empty())    {        int u=Q.front();Q.pop();        for(int v=0;v<G[u].size();v++)        {            Edge e=edges[G[u][v]^1];            if(!vis[e.from]&&e.cap>e.flow)            {                vis[e.from]=true;                d[e.from]=d[u]+1;                Q.push(e.from);            }        }    }}int augment(int s,int t){    int x=t,a=inf;    while(x!=s)    {        Edge e=edges[p[x]];        a=min(a,e.cap-e.flow);        x=e.from;    }    x=t;    while(x!=s)    {        edges[p[x]].flow+=a;        edges[p[x]^1].flow-=a;        x=edges[p[x]].from;    }    return a;}int maxflow(int s,int t){    int flow=0,u=s;    bfs(s,t);    memset(gap,0,sizeof(gap));    memset(cur,0,sizeof(cur));    for(int i=0;i<=t;i++) gap[d[i]]++;    while(d[s]<t+1)    {        if(u==t)        {            flow+=augment(s,t);            u=s;        }        bool flag=false;        for(int v=cur[u];v<G[u].size();v++) //Advance        {            Edge e=edges[G[u][v]];            if(e.cap>e.flow&&d[u]==d[e.to]+1)            {                flag=true;                p[e.to]=G[u][v];                cur[u]=v;                u=e.to;                break;            }        }        if(!flag) //Retreat        {            int m=t+1;            for(int v=0;v<G[u].size();v++)            {                Edge e=edges[G[u][v]];                if(e.cap>e.flow) m=min(m,d[e.to]);            }            if(--gap[d[u]]==0) break;            gap[d[u]=m+1]++;            cur[u]=0;            if(u!=s) u=edges[p[u]].from;        }    }    return flow;}int main(){    int T,n,k,id,num,no=0;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&k);        for(int i=1;i<=n;i++)        {            scanf("%d",&num);            for(int j=1;j<=num;j++)            {                scanf("%d",&id);                paste[i][id]++;            }        }        for(int i=1;i<=k;i++) //S->Bob            if(paste[1][i]) {addedge(0,i,paste[1][i]);}        for(int i=2;i<=n;i++) //Bob->friends            for(int j=1;j<=k;j++)            {                if(paste[i][j]>=2) addedge(k+i,j,paste[i][j]-1);                if(!paste[i][j]) addedge(j,k+i,1);            }        for(int i=1;i<=k;i++) addedge(i,k+n+1,1);//Bob->T        printf("Case #%d: %d\n",++no,maxflow(0,k+n+1));        for(int i=0;i<maxn;i++) G[i].clear();        edges.clear();        memset(paste,0,sizeof(paste));    }}

 

2817630 Neopenx Ultraviolet A 10779 Accepted 0 KB MS 16 C ++ 4.8.2 3270 B 13:48:15  

UV 10779 (max Stream)

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.