2012 成都网络赛


Control

Time Limit: 1000ms

Memory Limit: 32768KB

64-bit integer IO format: %I64d     
Java class name: Main

Submit Status PID:
26307

  You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from
one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.

  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.

  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.

  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you
must identify a set of cities, that:

  * all traffic of the terrorists must pass at least one city of the set.

  * sum of cost of controlling all cities in the set is minimal.

  You may assume that it is always possible to get from source of the terrorists to their destination.

------------------------------------------------------------

1 Weapon of Mass Destruction

Input

  There are several test cases.

  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.

  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.

  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 107.

  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.

  Please process until EOF (End Of File).

Output

  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.

  See samples for detailed information.

Sample Input

5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1

Sample Output

3

网络流: 点有权值费用,拆点建边。

#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef int MyType;
const int INF = 0x3F3F3F3F;
const int MAXN = 510;
const int MAXE = 100000 + 10;

struct Edge { int to, next; MyType cap; };
Edge es[MAXE];
int head[MAXN], cur[MAXN], level[MAXN], que[MAXN];
int n, m, src, des, cnt;

void add( int u, int v, MyType c ) {
    es[cnt].to = v; es[cnt].cap = c; es[cnt].next = head[u]; head[u] = cnt++;
    es[cnt].to = u; es[cnt].cap = 0; es[cnt].next = head[v]; head[v] = cnt++;
    return ;
}

bool bfs() {
    int mf, me;
    memset( level, 0, sizeof( level ) );
    mf = me = 0;
    que[me++] = src;
    level[src] = 1;
    while( mf < me ) {
        int u = que[mf++];
        for( int i = head[u]; ~i; i = es[i].next ) {
            int v = es[i].to;
            if( level[v] == 0 && es[i].cap > 0 ) {
                level[v] = level[u] + 1;
                que[me++] = v;
            }
        }
    }
    return ( level[des] != 0 );
}

int dfs( int u, int f ) {
    if( u == des || f == 0 ) return f;
    int flow = 0;
    for( int &i = cur[u]; ~i; i = es[i].next ) {
        Edge &e = es[i];
        if( e.cap > 0 && level[e.to] == level[u] + 1 ) {
            int d = dfs( e.to, min( f, e.cap ) );
            if( d > 0 ) {
                e.cap -= d;
                es[i ^ 1].cap += d;
                flow += d;
                f -= d;
                if( f == 0 ) break;
            } else level[e.to] = -1;
        }
    }
    return flow;
}

MyType dinic() {
    MyType ret = 0;
    while( bfs() ) {
        for( int i = 0; i <= 500; ++i ) {
            cur[i] = head[i];
        }
        ret += dfs( src, INF );
    }
    return ret;
}

int main()
{
    int a, b;
    while(~scanf("%d%d%d%d", &n, &m, &src, &des))
    {
        memset(head, -1, sizeof head);
        cnt = 0;
        int tt;
        des += 200;
        for( int i = 1; i <= n; ++i ) {
            scanf( "%d", &tt );
            add( i, i + 200, tt );
        }
        for( int i = 1; i <= m; ++i ) {
            scanf( "%d%d", &a, &b );
            add( a + 200, b, INF );
            add( b + 200, a, INF );
        }
        int ans = dinic();
        printf( "%d\n", ans );
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 12:13:46

2012 成都网络赛的相关文章

HDU 4291 A Short problem (2012成都网络赛,矩阵快速幂+循环节)

链接: click here~~ 题意: According to a research, VIM users tend to have shorter fingers, compared with Emacs users. Hence they prefer problems short, too. Here is a short one: Given n (1 <= n <= 1018), You should solve for g(g(g(n))) mod 109 + 7 where

HDU - 4734 F(x) (2013成都网络赛,数位DP)

题意:求0-B的满足<=F[A]的所有可能 思路:数位DP,记忆化搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -

hdu4271 Find Black Hand 2012长春网络赛E题 最短编辑距离

hdu4271 Find Black Hand  2012长春网络赛E题  最短编辑距离 Find Black Hand Time Limit : 5000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 1 Problem Description I like playing game with my friend

hdu 4465 Candy 2012 成都现场赛

1 /** 2 对于大数的很好的应用,,缩小放大,,保持精度 3 **/ 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 #include <cstdio> 8 using namespace std; 9 10 int main() 11 { 12 double n,p; 13 int cnt =1; 14 while(cin>>n>>p){ 15

hdu4405--Aeroplane chess(概率dp第七弹:飞行棋游戏--2012年网络赛)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1628    Accepted Submission(s): 1103 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids la

hdu 4737 A Bit Fun(2013 成都网络赛)

A Bit Fun                                                   Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2089    Accepted Submission(s): 1040 Problem Description There are n numbers in a arr

36th成都区域赛网络赛 hdoj4039 The Social Network(建图+字符串处理)

这题是某年成都区域赛网络赛的一题. 这题思路非常easy,可是从时间上考虑,不妨不要用矩阵存储,我用的链式前向星. 採用线上查询.利用map对字符串编号,由于非常方便.要推荐的朋友,事实上就是朋友的朋友(这里指的是直接朋友,图中即指有直接边相连的). 所以在寻找时,仅仅须要查找朋友的朋友,并计数. 注意:在输出时不能有对于的空格. 附代码: #include<iostream> using namespace std; #include<cstdio> #include<cs

hdu 4405 概率dp 2012年金华亚洲网络赛--虽然水,但是是自己独立做的第一道概率dp

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4405 e[i]:当前在位置i还需要走的步数期望 受刘汝佳的AC自动机那个后缀链接写法的启发,我的x[i]通过逆序算出来连续有"flight line "的时候,能到达的最远距离, rep(i,0,m) { scanf("%d%d",&xx,&yy); x[xx]=yy; } for(int i=n;i>=0;i--) if(x[i]!=-1 &

Go Deeper(2010成都现场赛题)(2-sat)

G - Go Deeper Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Here is a procedure's pseudocode: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep +