HDU 5352(MZL's City-费用流)

来源:互联网 发布:linux磁盘配额 编辑:程序博客网 时间:2024/05/21 11:18

MZL's City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 913    Accepted Submission(s): 331


Problem Description
MZL is an active girl who has her own country.

Her big country has N cities numbered from 1 to N.She has controled the country for so long and she only remebered that there was a big earthquake M years ago,which made all the roads between the cities destroyed and all 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 are connected with X directly and indirectly.Notice that if a city was rebuilt that it will 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 only knew that no more than K cities were rebuilt in one year.Now she only want to know the maximal number of cities that could be rebuilt.At the same time she want you to tell her the smallest lexicographically plan under the best answer.Notice that 8 2 1 is smaller than 10 0 1.
 

Input
The 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 country ,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 destoyed roads as (x,y).It’s guaranteed in any time there is no more than 1 road between every two cities and the road destoyed must exist in that time.
 

Output
The First line Ans is 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
30 2 1
Hint
No city was 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.
 

Author
SXYZ
 

Source
2015 Multi-University Training Contest 5
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5421 5420 5419 5418 5417 
 

费用流

二分图,左边m次操作,右边n个城市,求最优匹配



#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (1000+10)#define MAXM (100000*4+10)#define MAXT (50+10)#define MAXn (200+10)#define MAXm (500+10)#define MAXk (200+10)#define eps (1e-3)long long mul(long long a,long long b){return (a*b)%F;}long long add(long long a,long long b){return (a+b)%F;}long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}typedef long long ll;class Cost_Flow  {  public:      int n,s,t;      int q[10000];      int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;      double cost[MAXM];      void addedge(int u,int v,int w,double c)        {            edge[++size]=v;            weight[size]=w;            cost[size]=c;            next[size]=pre[u];            pre[u]=size;        }        void addedge2(int u,int v,int w,double c){addedge(u,v,w,c),addedge(v,u,0,-c);}       bool b[MAXN];      double d[MAXN];      int pr[MAXN],ed[MAXN];      bool SPFA(int s,int t)        {            For(i,n) d[i]=INF;          MEM(b)          d[q[1]=s]=0;b[s]=1;            int head=1,tail=1;            while (head<=tail)            {                int now=q[head++];                Forp(now)                {                    int &v=edge[p];                    if (weight[p]&&d[now]+cost[p]<d[v])                    {                        d[v]=d[now]+cost[p];                        if (!b[v]) b[v]=1,q[++tail]=v;                        pr[v]=now,ed[v]=p;                    }                }                b[now]=0;            }            return fabs(d[t]-INF)>eps;        }       double totcost;                  double CostFlow(int s,int t)        {            while (SPFA(s,t))            {                int flow=INF;                for(int x=t;x^s;x=pr[x]) flow=min(flow,weight[ed[x]]);                  totcost+=(double)flow*d[t];               for(int x=t;x^s;x=pr[x]) weight[ed[x]]-=flow,weight[ed[x]^1]+=flow;                 }            return totcost;        }        void mem(int n,int t)      {          (*this).n=n;          size=1;          totcost=0;          MEM(pre) MEM(next)       }  }S;  int n,m,k;bool b[MAXn][MAXn];int path[MAXm],cnt;bool vis[MAXn];void dfs(int x){vis[x]=1;For(i,n) if (b[x][i] && (!vis[i])) {dfs(i);} }int main(){//freopen("J.in","r",stdin);//freopen(".out","w",stdout);int T;cin>>T;while(T--) {MEM(b) MEM(path) cnt=0;cin>>n>>m>>k;S.mem(n+m+2,n+m+2);For(i,n) S.addedge2(1+m+i,n+m+2,1,0);For(i,m) {int p;scanf("%d",&p);if (p==1) {int x;scanf("%d",&x);path[++cnt]=S.size+2;S.addedge2(1,i+1,k,(double)m-i);MEM(vis) dfs(x);For(j,n) if ( vis[j]) S.addedge2(i+1,1+m+j,1, 0 );} else if (p==2) {int x,y;scanf("%d%d",&x,&y);b[x][y]=b[y][x]=1;} else {int l; scanf("%d",&l);while (l--) {int x,y;scanf("%d%d",&x,&y);b[x][y]=b[y][x]=0;}}}S.CostFlow(1,n+m+2);//Fork(i,2,S.size) cout<<S.weight[i]<<' '<<S.cost[i]<<endl;int ans=0;For(i,cnt) ans+=S.weight[path[i]];printf("%d\n",ans);For(i,cnt-1) printf("%d ",S.weight[path[i]]);printf("%d\n",S.weight[path[cnt]]);}return 0;}



0 0
原创粉丝点击