最新公告
  • 欢迎您光临波比源码,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入我们
  • 【最大流|关键边】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


    题意:由1个总部、若干个城市、中继站和1些网段构成1个容量网络。每一个网段是单向的且存在1定带宽。城市会无穷制的上传数据,求出该图中的这样1些网段:增加这些网段的带宽可以提高总部收到的总带宽。
    思路:建图――中继站和城市都是顶点,网段是边,总部是汇点。建立超级源点和每一个城市连边,容量为INF。
    要找出这个容量网络的关键边1,首先要跑1遍最大流,然后在残留网络中,分别从源点和汇点进行dfs和染色,如果该边不是满流就能够继续dfs。最后满流且两端点分别具有不同色彩2的边就是关键边。
    另外1定要注意边的真正条数。应当是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⑻;
    /****************************************/
    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. 仅仅具有不同色彩才能保证该边上增大的流量可以从源点到达汇点。 ?
    波比源码 – 精品源码模版分享 | www.bobi11.com
    1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
    2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
    3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
    4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
    5. 如有链接无法下载、失效或广告,请联系管理员处理!
    6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
    7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

    波比源码 » 【最大流|关键边】ZOJ-1532 Internship

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    波比源码
    一个高级程序员模板开发平台
    升级波友尊享更多特权立即升级