HDU 5352 MZL's City(2015 多校 第5场,最小费用最大流)

 

MZL's City

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


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
      
      
1 5 6 2 2 1 2 2 1 3 1 1 1 2 3 1 1 2 1 2
 

Sample Output
      
      
3 0 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.
 

Source
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#define LL long long
using namespace std;
const int maxn = 10000 + 10;
const int INF = 0x3f3f3f3f;
struct Edge
{
	int from, to, cap, flow, cost;
	Edge(int u, int v, int c, int f, int w) : from(u), to(v), cap(c), flow(f), cost(w) { }
};
int n;
vector<Edge>edges;
vector<int>G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
void init()
{
	for(int i=0;i<maxn;i++)
		G[i].clear();
	edges.clear();
}
void AddEdge(int from, int to, int cap, int cost)
{
	edges.push_back(Edge(from,to,cap,0,cost));
	edges.push_back(Edge(to,from,0,0,-cost));
	int M = edges.size();
	G[from].push_back(M-2);
	G[to].push_back(M-1);
}
bool SPFA(int s, int t, int& flow, LL& cost)
{
	for(int i=0;i<=n+1;i++)
 		d[i] = INF;
	memset(inq, 0, sizeof(inq));
	d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
	queue<int>Q;
	Q.push(s);
	while(!Q.empty())
	{
		int u = Q.front();Q.pop();
		inq[u] = 0;
		for(int i=0;i<G[u].size();i++)
		{
			Edge& e = edges[G[u][i]];
			if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
			{
				d[e.to] = d[u] + e.cost;
				p[e.to] = G[u][i];
				a[e.to] = min(a[u], e.cap - e.flow);
				if(!inq[e.to]){Q.push(e.to);inq[e.to] = 1;}
			}
		}
	}
	if(d[t] == INF) return false;
	flow += a[t];
	cost += (LL) d[t] * (LL) a[t];
	for(int u=t;u!=s;u=edges[p[u]].from)
	{
		edges[p[u]].flow += a[t];
		edges[p[u]^1].flow -= a[t];
	}
	return true;
}
int MincostMaxflow(int s, int t, LL& cost)
{
	int flow = 0; cost = 0;
	while(SPFA(s,t,flow,cost));
	return flow;
}
int dis[210][210], vis[210];
int N, M, K;
void dfs(int u)
{
    vis[u] = 1;
    for(int i=1;i<=N;i++)
    {
        if(!vis[i] && dis[u][i])
            dfs(i);
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        init();
        memset(dis, 0, sizeof(dis));
        scanf("%d%d%d", &N, &M, &K);
        int op, x, y;
        int s = 0, t = N + 1;
        int tt = N + 2, COST = M + 10;
        for(int i=1;i<=N;i++) AddEdge(i, t, 1, 0);
        for(int i=1;i<=M;i++)
        {
            scanf("%d", &op);
            if(op == 1)
            {
                memset(vis, 0, sizeof(vis));
                scanf("%d", &x);
                dfs(x);
                AddEdge(s, tt, K, COST);
                COST--;
                for(int j=1;j<=N;j++) if(vis[j])
                    AddEdge(tt, j, 1, 0);
                tt++;
            }
            else if(op == 2)
            {
                scanf("%d%d", &x, &y);
                dis[x][y] = dis[y][x] = 1;
            }
            else
            {
                int e; scanf("%d", &e);
                while(e--)
                {
                    scanf("%d%d", &x, &y);
                    dis[x][y] = 0;
                    dis[y][x] = 0;
                }
            }
        }
        n = tt;
        LL ans;
        int flow = MincostMaxflow(s, t, ans);
        printf("%d\n", flow);
        int flag = 0;
        for(int i=0;i<edges.size();i++)
        {
            if(flag == 0 && edges[i].from == s)
            {
                printf("%d", edges[i].flow);
                flag = 1;
            }
            else if(edges[i].from == s)
            {
                printf(" %d", edges[i].flow);
            }
        }
        printf("\n");
    }
    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值