UVALive 5099

B - Nubulsa Expo

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVALive 5099

Description

You may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa is an undeveloped country and it is threatened by the rising of sea level. Scientists predict that Nubulsa will disappear by the year of 2012. Nubulsa government wants to host the 2011 Expo in their country so that even in the future, all the people in the world will remember that there was a country named ``Nubulsa".

As you know, the Expo garden is made up of many museums of different countries. In the Expo garden, there are a lot of bi-directional roads connecting those museums, and all museums are directly or indirectly connected with others. Each road has a tourist capacity which means the maximum number of people who can pass the road per second.

Because Nubulsa is not a rich country and the ticket checking machine is very expensive, the government decides that there must be only one entrance and one exit. The president has already chosen a museum as the entrance of the whole Expo garden, and it‘s the Expo chief directory Wuzula‘s job to choose a museum as the exit.

Wuzula has been to the Shanghai Expo, and he was frightened by the tremendous ``people mountain people sea" there. He wants to control the number of people in his Expo garden. So Wuzula wants to find a suitable museum as the exit so that the ``max tourists flow" of the Expo garden is the minimum. If the ``max tourist flow" is W, it means that when the Expo garden comes to ``stable status", the number of tourists who enter the entrance per second is at most W. When the Expo garden is in ``stable status", it means that the number of people in the Expo garden remains unchanged.

Because there are only some posters in every museum, so Wuzula assume that all tourists just keep walking and even when they come to a museum, they just walk through, never stay.

Input

There are several test cases, and the input ends with a line of ``0 0 0".

For each test case:

The first line contains three integers NM and S, representing the number of the museums, the number of roads and the No. of the museum which is chosen as the entrance (all museums are numbered from 1 to N). For example, 5 5 1 means that there are 5 museums and 5 roads connecting them, and the No. 1 museum is the entrance.

The next M lines describe the roads. Each line contains three integers XY and K, representing the road connects museum X with museum Y directly and its tourist capacity is K.

Please note:

1 < N300, 0 < M50000, 0 < SXYN, 0 < K1000000

Output

For each test case, print a line with only an integer W, representing the ``max tourist flow" of the Expo garden if Wuzula makes the right choice.

Sample Input

5 5 1
1 2 5
2 4 6
1 3 7
3 4 3
5 1 10
0 0 0

Sample Output

8
/**
    最大流 == 最小割
**/
#include <iostream>
#include <string.h>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 505;
const ll maxw = 1000007;
const ll inf = 1e17;
ll g[N][N], w[N];
int a[N], v[N], na[N];
ll mincut(int n) {
    int i, j, pv, zj;
    ll best = inf;
    for(i = 0; i < n; i ++) {
        v[i] = i;
    }
    while(n > 1) {
        for(a[v[0]] = 1, i = 1; i < n; i ++) {
            a[v[i]] = 0;
            na[i - 1] = i;
            w[i] = g[v[0]][v[i]];
        }
        for(pv = v[0], i = 1; i < n; i ++) {
            for(zj = -1, j = 1; j < n; j ++)
                if(!a[v[j]] && (zj < 0 || w[j] > w[zj])) {
                    zj = j;
                }
            a[v[zj]] = 1;
            if(i == n - 1) {
                if(best > w[zj]) {
                    best = w[zj];
                }
                for(i = 0; i < n; i ++) {
                    g[v[i]][pv] = g[pv][v[i]] += g[v[zj]][v[i]];
                }
                v[zj] = v[--n];
                break;
            }
            pv = v[zj];
            for(j = 1; j < n; j ++) if(!a[v[j]]) {
                    w[j] += g[v[zj]][v[j]];
                }
        }
    }
    return best;
}
int main()
{
    int n, m, s;
    while(~scanf("%d %d", &n, &m))
    {
        for(int i = 0; i <= n; i++)
        {
            for(int j = 0; j <= n; j++)
            {
                g[i][j] = 0;
            }
        }
        int u, v, w;
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
             u--;
             v--;
            g[u][v] += w;
            g[v][u] += w;
        }
        printf("%lld\n", mincut(n));
    }
    return 0;
}
时间: 2024-10-25 09:07:08

UVALive 5099的相关文章

UVALive 5099 Nubulsa Expo 全局最小割 非网络流 n^3

题目链接:点击打开链接 题意: 给定n个点m条无向边 源点S 下面m行给出无向边以及边的容量. 问: 找一个汇点,使得图的最大流最小. 输出最小的流量. 思路: 最大流=最小割. 所以题意就是找全局最小割. 和源点无关,因为不关心源点在哪个点集里. 模版题: O(n^3) #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

UVALive 6467 Strahler Order 拓扑排序

这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ 以后能用CIN还是CIN吧 QAQ 贴代码了: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostre

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Gym 100299C &amp;&amp; UVaLive 6582 Magical GCD (暴力+数论)

题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点时,我们对gcd相同的只保留一个,那就是左端点最小的那个,只有这样才能保证是最大,然后删掉没用的. UVaLive上的数据有问题,比赛时怎么也交不过,后来去别的oj交就过了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000&qu

UVALive 6511 Term Project

Term Project Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 651164-bit integer IO format: %lld      Java class name: Main 解题:强连通分量 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

UVALive 5545 Glass Beads

Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 554564-bit integer IO format: %lld      Java class name: Main Once upon a time there was a famous actress. As you may expect, she played mostly