MZL ' s city (hdu 5352 minimum charge flow | | Binary graph matching)

Source: Internet
Author: User
Tags acos cmath

MZL ' s CityTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 719 Accepted Submission (s): 251


Problem Descriptionmzl is a active girl who had her own country.

Her big country have N cities numbered from 1 to N.she have controled the country for so long and She only remebered that th Ere is a big earthquake M years Ago,which made all the roads between the cities destroyed and all of the city became broken. She also remebered that exactly one of the following things happened every recent M years:

1.She rebuild Some cities that is connected with X directly and indirectly. Notice that if a city is rebuilt that it'll never be broken again.

2.There is a bidirectional road between City X and city Y built.

3.There is a earthquake happened and some roads were destroyed.

She forgot the exactly cities that were rebuilt,but she is only knew this no more than K cities were rebuilt in one year. Now she is want to know the maximal number of cities that could be rebuilt. At the same time she want we are the smallest lexicographically plan under the best answer. Notice that 8 2 1 is smaller than 10 0 1.
Inputthe first contains one integer T (t<=50), indicating the number of tests.

For each test,the first line contains three integers n,m,k (n<=200,m<=500,k<=200), indicating the number of MZL ' s C Ountry, the years happened a big earthquake and the limit of the rebuild. Next M Lines,each Line contains a Operation,and the format is "1 x", "2 x y", or a operation of type 3.

If it ' s type 3,first it is a interger p,indicating the number of the destoyed roads,next 2*p numbers,describing the P dest Oyed Roads as (x, y). It's guaranteed in any time there was no more than 1 road between every both cities and the road destoyed must exist in that Time.
Outputthe first line Ans was the maximal number of the city Rebuilt,the second line is a array of length of tot describing The plan you give (tot is the number of the operation of type 1).
Sample Input
15 6 22 1 2 2 1 31 11 23 1 1 21 2

Sample Output
2 1Hint No City is rebuilt in the third Year,city 1 and City 3 were rebuilt in the fourth Year,and City 2 was Rebuilt in the sixth year.

Source2015 multi-university Training Contest 5
Recommendwange2014 | We have carefully selected several similar problems for you:5363 5362 5361 5360 5359


Test instructions: There are n cities m years ago destroyed by the earthquake, the road was destroyed, m years carried out some urban and road reconstruction, there are three kinds of operations:

(1) 1 u means rebuilding U or a city connected directly or indirectly to u;

(2) 2 U-v means a road is built between the city U and V;

(3) 3 indicates which roads have been damaged by the earthquake.

The largest number of cities rebuilt every year, the city once rebuilt will not be destroyed by the earthquake, asked how many cities in total rebuilt after m years, and according to the dictionary sequence output of the number of cities built each year.

Idea: At first only know to pour, specifically how to do not think out, see the solution is to use a binary map matching, this is the first time I encountered the operation as a node, too weak, or the topic do less =-=. But it is easier to feel the cost flow, first of all to talk about the practice of cost flow.

The operation of rebuilding a city as a node I, add the source point and the sink point, the source point to I edge, the capacity is K, because to control the dictionary output, so the higher the cost of the higher, and then I to the other connected City edge, capacity is 1, the cost is 0, each city to the meeting point edge, capacity of 1, the cost is 0. After running the cost flow is to rebuild the largest number of cities, and then connected to the source of the traffic on the edge is the number of cities to be rebuilt each year, in turn output can.

Code:

Minimum cost flow # include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include < set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MOD 1000000009#define INF 0x3f3f3f3f#define Pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF prllf#define DBG pf ("hi\n") const int MAXN = 202;const int MAXN = 1010;const int maxm = 2000000;typedef __int64 ll;using namespace Std;int n,m,k,cnt;int Mp[maxn][maxn],node[500][max N];bool vis[maxn];struct edge{int To,next,cap, Flow,cost;}    Edge[maxm];int head[maxn],tol,tot;int pre[maxn],dis[maxn];bool vis[maxn];int n;void init () {tol=0; memset (head,-1,sizeof (Head));}    void Addedge (int u,int v,int cap,int cost) {edge[tol].to=v;    Edge[tol].cap=cap;    Edge[tol].cost=cost;    edge[tol].flow=0;    Edge[tol].next=head[u];    head[u]=tol++;    Edge[tol].to=u;    Edge[tol].cap=0;    Edge[tol].cost=-cost;    edge[tol].flow=0;    EDGE[TOL].NEXT=HEAD[V]; head[v]=tol++;}    BOOL SPFA (int s,int t) {queue<int>q;        for (int i=0;i<n;i++) {dis[i]=inf;        Vis[i]=false;    Pre[i]=-1;    } dis[s]=0;    Vis[s]=true;    Q.push (s);        while (!q.empty ()) {int U=q.front ();        Q.pop ();        Vis[u]=false;            for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].to; if (Edge[i].cap > Edge[i].flow && dis[v] > Dis[u] + edge[i].cost) {Dis[v]=dis[u]                + Edge[i].cost;                Pre[v]=i;if (!vis[v]) {vis[v]=true;                Q.push (v);    }}}} if (Pre[t]==-1) return false; else return true;}    The maximum flow is returned, the cost is the minimum charge int mincostmaxflow (int s,int t,int &cost) {int flow=0;    cost=0;        while (SPFA (s,t)) {int min=inf; for (int i=pre[t];i!=-1;i=pre[edge[i^1].to]) {if (min > Edge[i].cap-edge[i].flow) Min        =edge[i].cap-edge[i].flow;            } for (int i=pre[t];i!=-1;i=pre[edge[i^1].to]) {edge[i].flow+=min;            Edge[i^1].flow-=min;        Cost+=edge[i].cost*min;    } flow+=min; } return flow;}    void Dfs (int u) {vis[u]=true;    Node[cnt][tot++]=u; for (int v=1;v<=n;v++) if (! VIS[V]&AMP;&AMP;MP[U][V]) Dfs (v);}    int main () {int i,j,t,u,v,op,cc;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n,&m,&k);  Memset (Mp,0,sizeof (MP));      Init (); cnt=0;        cc=500;            for (i=0;i<m;i++) {scanf ("%d", &op);                if (op==1) {scanf ("%d", &u);                memset (vis,false,sizeof (VIS));                Tot=1;                ++cnt;                DFS (U);                node[cnt][0]=tot-1;                Addedge (0,CNT+N,K,CC);            cc--;                } else if (op==2) {scanf ("%d%d", &u,&v);            Mp[u][v]=mp[v][u]=1;                } else {int xx;                scanf ("%d", &xx);                    while (xx--) {scanf ("%d%d", &u,&v);                mp[u][v]=mp[v][u]=0;        }}} n=n+cnt+2;        for (i=1;i<=cnt;i++) {for (j=1;j<=node[i][0];j++) Addedge (i+n,node[i][j],1,0); } for (i=1;i<=n;i++) Addedge (i,cnt+n+1,1,0);        int ans,cost;        Ans=mincostmaxflow (0,n+cnt+1,cost);        printf ("%d\n", ans);        printf ("%d", edge[0].flow);        for (i=2;i<2*cnt;i+=2) printf ("%d", edge[i].flow);    printf ("\ n"); }}

Another two of the practice, the operation of each year into a K-node, each node to the point of connection to the Unicom, to make a match from the back to ensure that the dictionary order is minimal. I started with a multi-match, WA, and saw someone else do it this way.

Code:

Two-part map # include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include < set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MOD 1000000009#define INF 0x3f3f3f3f#define Pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF prllf#define DBG pf ("hi\n") const int MAXN = 202;const int MAXN = 100010;const int maxm = 2000000;typedef __int64 ll;using namespace std;struct edge{int u,v,next;} Edge[maxm];int Linker[maxn];bool Used[maxn];int Num,head[maxn],anS[maxn];int Mp[maxn][maxn];int node[maxn];//deposit with u connected point bool Vis[maxn];int n,m,k,cnt,tot;void init () {num=0; for (int i=0;i<=m*k;i++) head[i]=-1;}    void Addedge (int u,int v) {edge[num].u=u;    Edge[num].v=v;    Edge[num].next=head[u]; head[u]=num++;}        bool Dfs (int u) {for (int i=head[u];~i;i=edge[i].next) {int v=edge[i].v;            if (!used[v]) {used[v]=true; if (linker[v]==-1| |                DFS (Linker[v])) {linker[v]=u;            return true; }}} return false;}    int Hungary () {int res=0;    memset (LINKER,-1,SIZEOF (linker));    memset (ans,0,sizeof (ans)); for (int i=cnt-1;i>=0;i--) {for (int j=i*k;j<i*k+k;j++) {memset (used,false,sizeof (used            ));                if (Dfs (j)) {res++;            ans[i]++; }}} return res;}    void DFS (int u) {vis[u]=true;    Node[tot++]=u;     for (int v=1;v<=n;v++)   if (!vis[v]&&mp[u][v]) DFS (v);}    int main () {int i,j,k,t,op,u,v;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n,&m,&k);        Init ();        Memset (Mp,0,sizeof (MP));  cnt=0;            Record the number of operations 1 for (i=0;i<m;i++) {scanf ("%d", &op);                if (op==1) {scanf ("%d", &u);                memset (vis,false,sizeof (VIS));                tot=0;     DFS (U); DFS Search and U-connected points coexist within the node array for (j=cnt*k;j<cnt*k+k;j++) {for (k=0;k<to                t;k++) Addedge (J,node[k]);            } cnt++;                } else if (op==2) {scanf ("%d%d", &u,&v);            Mp[u][v]=mp[v][u]=1;                } else {int xx;                scanf ("%d", &xx); while (xx--) {scanf ("%d%d ", &u,&v);                mp[u][v]=mp[v][u]=0;        }}} printf ("%d\n", Hungary ());        printf ("%d", ans[0]);        for (i=1;i<cnt;i++) printf ("%d", ans[i]);    printf ("\ n"); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MZL ' s city (hdu 5352 minimum charge flow | | Binary graph matching)

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.