MZL's City(网络流)

本文介绍了一种利用网络流算法解决城市重建规划问题的方法。面对地震后的重建挑战,通过构建特殊的图模型并运用dinic算法求解最大流,以此确定最多能重建的城市数量及最优重建计划。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MZL's City

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



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. 题意: 一个国家原本有n个城市和若干条联通城市的路,可是在M年前,因为一场大地震,整个国家都被摧毁了. 现在打算在m年里,重建国家,有三个操作: 1.> 输入1,x,表示可以修建包含x在内的间接或者直接相连的城市. 2.> 输入2,x,y表示新建x到y的无向边 3.> 输入3,p,表示要摧毁p条道路,接下来输入2p个数,分别表示p条路; 最后问最终能够修建多少个城市.和输出一个字典序最小的方案; 题解: 网络流可以做; 把年份分为一边,城市分为另一边,连边构图; 从s往所有年份连一条容量为k的边,所有城市往汇点t连一条容量为1的边; 其他的点,按照关系连一条容量为1的边; 因为题目要求以最小字典序输出,所以我们从后往前做网络流; AC代码:
/* ***********************************************
Author        :xdlove
Created Time  :2015年07月31日 星期五 12时37分29秒
File Name     :a.cpp
 ************************************************ */

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

/**宏定义类
 * **/
#define FOR(i,s,t) for(int i = (s); i < (t); i++)
#define FOR_REV(i,s,t) for(int i = (s - 1); i >= (t); i--)
#define mid ((l + r) >> 1)
#define clr(a) memset(a,0,sizeof(a))
#define lson l,mid,u<<1
#define rson mid+1,r,u<<1|1
#define ls u<<1
#define rs u<<1|1

typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0);

/**输入输出挂类模板
 * **/

class Fast
{
    public:
        inline void rd(int &ret)
        {
            char c;
            int sgn;
            if(c = getchar(),c == EOF) return;
            while(c != '-' && (c < '0' || c > '9')) c = getchar();
            sgn = (c == '-') ? -1 : 1;
            ret = (c == '-') ? 0 : (c - '0');
            while(c = getchar(),c >= '0' && c <= '9')
                ret = ret * 10 + c - '0';
            ret *= sgn;
        }

    public:
        inline void pt(int x)
        {
            if(x < 0)
            {
                putchar('-');
                x = -x;
            }
            if(x > 9) pt(x / 10);
            putchar(x % 10 + '0');
        }
};
Fast xd;

const int MAXM = 1e6;
const int MAXN = 1000;



struct Node
{
    int from,to,next;
    int cap;
}edge[MAXM * 2];
int tol;
int Head[MAXN];
int que[MAXN];
int dep[MAXN]; //dep为点的层次
int stack[MAXN];//stack为栈,存储当前增广路
int cur[MAXN],cnt;//存储当前点的后继

void Init()
{
    tol = cnt = 0;
    memset(Head,-1,sizeof(Head));
}

void add_edge(int u, int v, int w)
{
    //printf("%d: %d: %d\n",u,v,w);
    edge[tol].from = u;
    edge[tol].to = v;
    edge[tol].cap = w;
    edge[tol].next = Head[u];
    Head[u] = tol++;
    edge[tol].from = v;
    edge[tol].to = u;
    edge[tol].cap = 0;
    edge[tol].next = Head[v];
    Head[v] = tol++;
}

int BFS(int start, int end)
{
    int front, rear;
    front = rear = 0;
    memset(dep, -1, sizeof(dep));
    que[rear++] = start;
    dep[start] = 0;
    while (front != rear)
    {
        int u = que[front++];
        if (front == MAXN)front = 0;
        for (int i = Head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if (edge[i].cap > 0 && dep[v] == -1)
            {
                dep[v] = dep[u] + 1;
                que[rear++] = v;
                if (rear >= MAXN)rear = 0;
                if (v == end)return 1;
            }
        }
    }
    return 0;
}

int dinic(int start, int end)
{
    int res = 0;
    int top;
    while (BFS(start, end))
    {
        memcpy(cur, Head, sizeof(Head));
        int u = start;
        top = 0;
        while (true)
        {
            if (u == end)
            {
                int min = INF;
                int loc;
                for (int i = 0; i < top; i++)
                    if (min > edge[stack[i]].cap)
                    {
                        min = edge[stack[i]].cap;
                        loc = i;
                    }
                for (int i = 0; i < top; i++)
                {
                    edge[stack[i]].cap -= min;
                    edge[stack[i] ^ 1].cap += min;
                }
                res += min;
                top = loc;
                u = edge[stack[top]].from;
            }
            for (int i = cur[u]; i != -1; cur[u] = i = edge[i].next)
                if (edge[i].cap != 0 && dep[u] + 1 == dep[edge[i].to])
                    break;
            if (cur[u] != -1)
            {
                stack[top++] = cur[u];
                u = edge[cur[u]].to;
            }
            else
            {
                if (top == 0)break;
                dep[u] = -1;
                u = edge[stack[--top]].from;
            }
        }
    }
    return res;
}

int g[205][205],used[205];
int Stack[600];
int n,m,k;

void dfs(int u)
{
    used[u] = 1;
    add_edge(cnt,u + m,1);
    FOR(v, 1, 1 + n)
    {
        if(g[u][v] && !used[v])
        {
            used[v] = 1;
            dfs(v);
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);        
    int T;
    xd.rd(T);
    while(T--)
    {
        Init();
        xd.rd(n);
        xd.rd(m);
        xd.rd(k);
        clr(g);
        clr(used);
        vector<int> p;
        FOR(i, 0, m)
        {
            int o,x,y;
            xd.rd(o);
            if(o == 2)
            {
                xd.rd(x);
                xd.rd(y);
                g[x][y] = g[y][x] = 1;
            }
            else if(o == 3)
            {
                xd.rd(o);
                FOR(i, 0, o)
                {
                    xd.rd(x);
                    xd.rd(y);
                    g[x][y] = g[y][x] = 0;
                }
            }
            else 
            {
                cnt++;
                clr(used);
                p.push_back(cnt);
                xd.rd(x);
                dfs(x);
            }
        }
        int s = 0,t = n + m + 1;
        for(int i = 1; i <= n; i++)
            add_edge(i + m,t,1);
        int pos = 0,sum = 0;
        for(vector<int> :: iterator it = --p.end(); ; it--)
        {
            int id = *it;
            //cout<<id<<endl;
            add_edge(s,id,k);
            Stack[pos++] = dinic(s,t);
            sum += Stack[pos - 1];
            if(it == p.begin()) break;
        }
        printf("%d\n",sum);
        FOR_REV(i,pos,0)
        {
            if(i != pos - 1) putchar(' ');
            printf("%d",Stack[i]);
        }
        puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值