【最大流|关键边】ZOJ-1532 Internship

Internship

Time Limit: 5 Seconds Memory Limit: 32768 KB

CIA headquarter collects data from across the country through its classified network. They have been using optical fibres long before it’s been deployed on any civilian projects. However they are still under a lot pressure recently because the data are growing rapidly. As a result they are considering upgrading the network with new technologies that provide a few times wider bandwidth. In the experiemental stage, they would like to upgrade one segment of their original network in order to see how it performs. And as a CIA intern it’s your responsibility to investigate which segment could actually help increase the total bandwidth the headquarter receives, suppose that all the cities have infinite data to send and the routing algorithm is optimized. As they have prepared the data for you in a few minutes, you are told that they need the result immediately. Well, practically immediately.

Input

Input contains multiple test cases. First line of each test case contains three integers n, m and l, they represent the number of cities, the number of relay stations and the number of segments. Cities will be referred to as integers from 1 to n, while relay stations use integers from n+1 to n+m. You can saves assume that n + m <= 100, l <= 1000 (all of them are positive). The headquarter is identified by the integer 0.

The next l lines hold a segment on each line in the form of a b c, where a is the source node and b is the target node, while c is its bandwidth. They are all integers where a and b are valid identifiers (from 0 to n+m). c is positive. For some reason the data links are all directional.

The input is terminated by a test case with n = 0. You can safely assume that your calculation can be housed within 32-bit integers.

Output

For each test print the segment id’s that meets the criteria. The result is printed in a single line and sorted in ascending order, with a single space as the separator. If none of the segment meets the criteria, just print an empty line. The segment id is 1 based not 0 based.

Sample Input

2 1 3

1 3 2

3 0 1

2 0 1

2 1 3

1 3 1

2 3 1

3 0 2

0 0 0

Sample Output

2 3



题意:由一个总部、若干个城市、中继站以及一些网段构成一个容量网络。每个网段是单向的且存在一定带宽。城市会无限制的上传数据,求出该图中的这样一些网段:增加这些网段的带宽可以提高总部收到的总带宽。

思路:建图——中继站和城市都是顶点,网段是边,总部是汇点。建立超级源点和每个城市连边,容量为INF。

要找出这个容量网络的关键边1,首先要跑一遍最大流,然后在残留网络中,分别从源点和汇点进行dfs以及染色,如果该边不是满流就可以继续dfs。最后满流且两端点分别拥有不同颜色2的边就是关键边。

另外一定要注意边的真正条数。应该是3000.

代码如下

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 100 + 5, M = 3e3 + 5;
int n, m, l, tot, src, sink;
struct Edge {
    int u, v, w, next;
    Edge(){}
    Edge(int _u, int _v, int _w, int _next):
        u(_u), v(_v), w(_w), next(_next){}
}e[M];
int head[N], cur[N], s[N], lev[N];
int line[M];
bool vis1[N], vis2[N];

void init()
{
    memset(head, -1, sizeof(head));
    memset(vis1, 0, sizeof(vis1));
    memset(vis2, 0, sizeof(vis2));
    tot = 0;
}

void add(int u, int v, int w)
{
    e[tot] = Edge(u, v, w, head[u]);
    head[u] = tot++;
    e[tot] = Edge(v, u, 0, head[v]);
    head[v] = tot++;
}

bool bfs()
{
    queue <int> q;
    memset(lev, -1, sizeof(lev));
    lev[src] = 0;
    q.push(src);
    while(!q.empty()) {
        int u = q.front(); q.pop();
        for(int i = head[u]; ~i; i = e[i].next) {
            int v = e[i].v;
            if(e[i].w && lev[v] == -1) {
                lev[v] = lev[u] + 1;
                q.push(v);
                if(v == sink) return true;
            }
        }
    }
    return false;
}

int Dinic()
{
    int ret = 0;
    while(bfs()) {
        memcpy(cur, head, sizeof(cur));
        int u = src, top = 0;
        while(1) {
            if(u == sink) {
                int mini = INF, loc;
                for(int i = 0; i < top; i++) {
                    if(mini > e[s[i]].w) {
                        mini = e[s[i]].w;
                        loc = i;
                    }
                }
                for(int i = 0; i < top; i++) {
                    e[s[i]].w -= mini;
                    e[s[i]^1].w += mini;
                }
                ret += mini;
                top = loc;
                u = e[s[top]].u;
            }
            int &i = cur[u];
            for(; ~i; i = e[i].next) {
                int v = e[i].v;
                if(e[i].w && lev[v] == lev[u] + 1) break;
            }
            if(~i) {
                s[top++] = i;
                u = e[i].v;
            }
            else {
                if(!top) break;
                lev[u] = -1;
                u = e[s[--top]].u;
            }
        }
    }
    return ret;
}

void dfs1(int u)
{
    vis1[u] = 1;
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(!vis1[v] && e[i].w) dfs1(v);
    }
}

void dfs2(int u)
{
    vis2[u] = 1;
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(!vis2[v] && e[i^1].w) dfs2(v);
    }
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    while(scanf("%d%d%d", &n, &m, &l), n) {
        int u, v, w;
        init();
        src = n+m+1; sink = 0;
        for(int i = 1; i <= n; i++) {
            add(src, i, INF);
        }
        for(int i = 0; i < l; i++) {
            scanf("%d%d%d", &u, &v, &w);
            line[i] = tot;//存储每条网段,省去了编号过程
            add(u, v, w);
        }
        Dinic();
        dfs1(src); dfs2(sink);
        bool fir = false;
        for(int i = 0; i < l; i++) {
            if(!e[line[i]].w && vis1[e[line[i]].u] && vis2[e[line[i]].v]) {
                if(fir) printf(" "); fir = true;
                printf("%d", i+1);
            }
        }
        puts("");
    }
    return 0;
}

  1. 如果仅对该边的容量进行增减,最大流就会因此增减。 ?
  2. 仅仅拥有不同颜色才能保证该边上增大的流量可以从源点到达汇点。 ?
时间: 2024-10-09 11:17:19

【最大流|关键边】ZOJ-1532 Internship的相关文章

ZOJ 1532 Internship (Dinic)

看来模板又错了,敲你妈妈小饼干 #include<iostream> #include<queue> #include<cstring> #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #include<stack> #include<vector> #include<map> using na

POJ 3204 Ikki&#39;s Story I - Road Reconstruction 最大流关键边

题目大意:给出一个裸的最大流的图,求这个图中哪一条边的流量增大会使整个图的最大流增大. 前言:POJ400题达成~~~ 思路:真心不知道这个题用预流推进怎么做,先给写预流推进的犇们点根蜡.. 我用的是Dinic,写起来就比较轻松.模拟一下Dinic的过程,加入一条边的流量增大就会使S到T的最大流增大的充要条件是 1.S->当前边的起始节点可以在残余网络中联通 2.当前边的终止节点->T可以在参与网络中联通 这样的话,增加了这条边的流量,就会有至少一的流量从S流到T,也就是增加了整个图的最大流.

ZOJ 2532 Internship

Internship Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 2532 64-bit integer IO format: %lld      Java class name: Main CIA headquarter collects data from across the country through its classified network.

ZOJ 2532 Internship 求隔边

Internship Time Limit: 5 Seconds      Memory Limit: 32768 KB CIA headquarter collects data from across the country through its classified network. They have been usingoptical fibres long before it's been deployed on any civilian projects. However the

kebab (hdu 2883 网络流判满流 关键是缩点)

kebab Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1071    Accepted Submission(s): 447 Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on

最大流算法 Dinic HDU 1532

#include <iostream> #include <cstring> #include <queue> #include <cstdio> using namespace std; #define V 205 #define E 205 #define INF 0x7ffffff struct Edge { int u, v, c, next; } e[E<<2]; int n, m, s, t; int d[V], head[V], c

POJ 3422 Kaka&#39;s Matrix Travels (最小费用最大流)

POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem?id=3422 题意:有一个N*N的方格,每个方格里面有一个数字.现在卡卡要从左上角走到右下角,规定每次只能向下或者向右走,每次走到一个格子,将得到该格子的数字,并且该格子的数字变为0.当卡卡走一次时,很容易求出最大值,问卡卡走k次,能够得到的最大值为多少. 思路:最小费用最大流 关键是如何构图 1. 将N*N个格点拆分为两个点(i,i + N*N),每个点之间连一条流量为1,费用为

网络流(进阶)

最大流:DINIC or SAP 最小费用最大流:SPFA+增广(费用的值较离散) or ZKW(费用的值集中) 有源汇的上下界最大流:新建s', t',用(i, j, l, r)表示i到j有一条下界为l上界为r的边,将每条这样的边拆成(s', j, 0, l), (i, t', 0, l), (i, j, 0, r-l),加入边(t, s, 0, max)再从s'到t'求最大流,再去掉(t, s, 0, max)这条边,从s到t求最大流 有源汇的上下界最小可行流:基本同上,将最后一步改成从t到

[转] 一些图论、网络流入门题总结、汇总

最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意:经典问题:K短路解法:dijkstra+A*(rec),方法很多相关:http://acm.pku.edu.cn/JudgeOnline/showcontest?contest_id=1144该题亦放在搜索推荐题中 POJ 3013 - Big Christmas Tree(基础)http://ac