UVA 10330 Power Transmission

题意:懒得打了。LUCKY CAT 里有 http://163.32.78.26/homework/q10330.htm

第一个网络流题目。每个节点都有一个容量值。需要拆点。拆成i - > i + N  边权为容量值

另外注意B个点 连接方式:s - 集合B

D个点 链接方式 集合D + N -> t汇点

其他看处理就好

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 250
const int INF = 0x3f3f3f3f ;
int flow[MAXN][MAXN],c[MAXN][MAXN];
int p[MAXN];
int N,M,B,D,src,tag;
void read()
{
    memset(c,0,sizeof(c));
    src = 0; tag = 2 * N + 1;
    for (int i = 1; i <= N; i++)
    {
        int tmp;
        scanf("%d",&tmp);
        c[i][i + N] = tmp;
    }
    scanf("%d",&M);
    for (int i = 1; i <= M; i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        c[u + N][v] = w;
    }
    scanf("%d%d",&B,&D);
    for (int i = 1; i <= B; i++)
    {
        int tmp;
        scanf("%d",&tmp);
        c[0][tmp] = INF;
    }
    for (int i = 1; i <= D; i++)
    {
        int tmp;
        scanf("%d",&tmp);
        c[tmp + N][tag] = INF;
    }
}
int Edmonds_karp(int src,int tag)
{
    memset(flow,0,sizeof(flow));
    int ans = 0;
    queue<int>q;
    while (!q.empty()) q.pop();
    int a[MAXN];
    while (true)
    {
        memset(a,0,sizeof(a));
        a[src] = INF;
        q.push(src);
        while (!q.empty())
        {
            int u = q.front(); q.pop();
            for (int v = 0; v <= tag; v++)
                if (!a[v] && c[u][v] > flow[u][v])
            {
                p[v] = u;
                q.push(v);
                a[v] = min(a[u],c[u][v] - flow[u][v]);
            }
        }
        if (a[tag] == 0) break;
        for (int u = tag; u != src; u = p[u])
        {
            flow[p[u]][u] += a[tag];
            flow[u][p[u]] -= a[tag];
        }
        ans += a[tag];
    }
    return ans;
}
int main()
{
    //freopen("sample.txt","r",stdin);
    while (scanf("%d",&N)!=EOF)
    {
        read();
        printf("%d\n",Edmonds_karp(src,tag));
    }
    return 0;
}
时间: 2024-10-10 06:55:55

UVA 10330 Power Transmission的相关文章

uva 10330 Power Transmission (最大流 + 拆点)

uva 10330 Power Transmission 如果对最大流不熟悉的话可以先去看看这个 题目大意:先来讲解一下INPUT.首先读入一个正整数N, 接下来N个数据是N个调节器的容量:然后读入一个正整数M, 接下来M组数据代表的是M条调节器与调解器之间的线路(调节器u, 调节器v, 容量);最后的一组数据先是两个正整数a和b, 接下来的a个数据代表的是初始的调节器,最后的b个数据代表的是终结的调节器. 综上,求最大流. 解题思路:源点或汇点不唯一的时候,记得要增加超级源点或超级汇点来使得源

UVA 10330 Power Transmission(网络最大流)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1271  Power Transmission  The Problem DESA is taking a new project to transfer power. Power is generated by the newly established plant in Barisal.

UVa 10298 - Power Strings

题目:求一个串的最大的循环次数. 分析:dp,KMP,字符串.这里利用KMP算法. KMP的next函数是跳跃到最近的串的递归结构位置(串元素取值0 ~ len-1): 由KMP过程可知: 如果存在循环节,则S[0 ~ next[len]-1] 与 S[len-next[len] ~ len-1]相匹配: 则S[next[len] ~ len-1]就是循环节(且最小),否则next[len]为0: 因此,最大循环次数为len/(len-next[len]),最小循环节为S[next[len] ~

UVA 11149 - Power of Matrix(矩阵倍增)

UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞,∑kiAi=(1+Ak/2)∑k/2iAi,不断二分即可 代码: #include <cstdio> #include <cstring> const int N = 45; int n, k; struct mat { int v[N][N]; mat() {memset(v, 0, sizeof(v));} mat operator * (mat

UVA - 10298 Power Strings (KMP求字符串循环节)

Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiati

[2016-03-03][UVA][1374][Power Calculus]

[2016-03-03][UVA][1374][Power Calculus] 时间:2016-03-03 16:14:01 星期四 题目编号:UVA 1374 题目大意:给出x的指数n,问,x经过多少次相乘才能得到x^n 输入:n 输出:次数 分析: 求乘法的所有可能方式,用dfs,适当剪枝优化,变成IDA* x的乘法,变成了指数的加法 每次不断平方,最少次数 为 不断平方的次数.这个为maxd起点 方法: 枚举每一位出现过的次数,当前字数加上枚举出来的次数,进入下一层dfs 剪枝:如果预计最

UVA 10298 - Power Strings(KMP)

UVA 10298 - Power Strings 题目链接 题意:本意其实就是,给定一个字符串,求出最小循环节需要几次循环出原字符串 思路:利用KMP中next数组的性质,n - next[n]就是最小循环节,然后n / 循环节就是答案 代码: #include <cstdio> #include <cstring> const int N = 1000005; char str[N]; int next[N]; void getnext() { int n = strlen(s

light oj 1155 - Power Transmission【拆点网络流】

1155 - Power Transmission   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB DESA is taking a new project to transfer power. Power is generated by the newly established plant in Barisal. The main aim of this project is to tr

Uva 11149 - Power of Matrix ( 矩阵快速幂 )

Uva 11149 -Power of Matrix ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 10 #define MAX_SIZE 40 struct Mat { int r, c; int mat[MAX_SIZE][MAX_SIZE]; Mat( int _r = 0 , int _c = 0 ) { CLR