2012 All online Races

Source: Internet
Author: User

Controltime limit:1000msmemory limit:32768kb64-bit integer IO format:%i64dJava class Name:MainSubmit 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 WMD1From one city (the source) to another one (the destination). You know their date, source and destination, and they is using the highway network.
The highway network consists of bidirectional highways, connecting and distinct city. A vehicle can only enter/exit the highway network at cities.
Locate some SA (special agents) in some selected cities, so, then the terrorists enter a city under Observatio N (that is, SA was in this city), they would be caught immediately.
It is possible-locate SA in all cities, but since controlling a city with SA could cost your department a certain amount of money, which might vary from city to city, and your budget might not being able to bear the full cost of controlling all C Ities, you must identify a set of cities, which:
* 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 could assume that it's always possible-get from source of the terrorists to their destination.
------------------------------------------------------------
1Weapon of Mass Destructioninput there is several test cases.
The first line of a single test case contains-N and M (2 <= n <=; 1 <= M <= 20000), the Numbe R of cities and the number of highways. Cities is numbered from 1 to N.
The second line contains the S,d (1 <= s,d <= N), the number of the source and the number of the Destinatio N.
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 Observati On. May assume, the cost are positive and not exceeding 107.
The FOLLOWINGM lines tells you about highway network. Each of these lines contains, integers a and B, indicating a bidirectional highway between A and B.
Please process until EOF (End of File).
Output for all test case should output exactly one line, containing one integer, the sum of the cost of your selected SE T.
See samples for detailed information. Sample Input
5 65 35234121 55 42 32 44 32 1
Sample Output
3

Network flow: Point entitlement value fee, split to build edge.

#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);    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 <=; ++i) {cur[i] = Head[i];    } ret + = DFS (src, INF); } return ret;}    int main () {int A, B;        while (~SCANF ("%d%d%d%d", &n, &m, &AMP;SRC, &des)) {memset (head,-1, sizeof head);        CNT = 0;        int tt;        Des + = 200;            for (int i = 1; I <= n; ++i) {scanf ("%d", &AMP;TT); Add (i, I+ $, TT);            } for (int i = 1; I <= m; ++i) {scanf ("%d%d", &a, &b);            Add (A + A, B, INF);        Add (b + N, a, INF);        } int ans = dinic ();    printf ("%d\n", ans); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

2012 All online Races

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.