HDU 2435 There is a war (minimum cut for modifying or adding an edge) Classic

Source: Internet
Author: User

HDU 2435 There is a war (minimum cut for modifying or adding an edge) Classic
There is a warTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 952 Accepted Submission (s): 269



Problem Description There is a sea.
There are N islands in the sea.
There are some directional bridges connecting these islands.
There is a country called Country One located in Island 1.
There is another country called Country Another located in Island N.
There is a war against Country Another, which launched by Country One.
There is a strategy which can help Country Another to defend this war by destroying the bridges for the purpose of making Island 1 and Island n disconnected.
There are some different destroying costs of the bridges.
There is a prophet in Country Another who is clever enough to find the minimum total destroying costs to achieve the strategy.
There is an architecture in Country One who is capable enough to rebuild a bridge to make it unbeatable or build a new invincvisible directional bridge between any two countries from the subset of island 2 to island n-1.
There is not enough time for Country One, so it can only build one new bridge, or rebuild one existing bridge before the Country Another starts destroying, or do nothing if happy.
There is a problem: Country One wants to maximize the minimum total destroying costs Country Another needed to achieve the strategy by making the best choice. Then what's the maximum possible result?

Input There are multiple cases in this problem.
There is a line with an integer telling you the number of cases at the beginning.
The are two numbers in the first line of every case, N (4 <= N <= 100) and M (0 <= M <= n * (n-1)/2 ), indicating the number of islands and the number of bridges.
There are M lines following, each one of which contains three integers a, B and c, with 1 <= a, B <= N and 1 <= c <= 10000, meaning that there is a directional bridge from a to B with c being the destroying cost.
There are no two lines containing the same a and B.

Output There is one line with one integer for each test case, telling the maximun possible result.

Sample Input

44 04 21 2 23 4 24 31 2 12 3 13 4 104 31 2 52 3 23 4 3

Sample Output
0213

Source 2008 Asia Chengdu Regional Contest Online question: For a directed graph, the cost of the removed edge is the edge weight. Now, 1 is about to reach n, and n is about to stop 1 from arriving, the condition is that 1 can only modify or add one edge at any two points (not including 1 and n) (this edge cannot be split), and the minimum cost of n is calculated.
Problem solving: If there are no conditions, it is the simplest minimal cut, but now a condition is added to modify or add an edge. First, find the initial minimum cut, and divide n points into two parts: connected to the sink point n and connected to the source point. We first use a bfs to mark the points that are connected to the source point, so the points that are not marked may be connected to the sink point. Let's analyze: if the two points of an edge are modified or added, either the two points are connected to the source point or the sink point, the minimum cut will not change, therefore, the two vertices (u, v) of the modified or newly added edge can only be u, which is connected to the Source Vertex, v is connected to the sink vertex, And the edge weight is changed to INF, in this way, the minimum cut may become larger. If you want to modify an existing edge, remember to change it back to the original value after obtaining the minimum cut. If it is a new edge, you need to change the edge weight to 0.
# Include
 
  
# Include
  
   
# Include
   
    
# Define deusing namespace std; # define captype intconst int MAXN = 110; // the total number of points const int MAXM = 400010; // The total number of sides const int INF = 1 <30; struct EDG {int to, next; captype cap, flow;} edg [MAXM]; int eid, head [MAXN]; int gap [MAXN]; // The number of each distance (or height) Point int dis [MAXN]; // The shortest distance between each point and the end eNode int cur [MAXN]; // cur [u] indicates that starting from the u point can flow through the cur [u] side int pre [MAXN]; void init () {eid = 0; memset (head, -1, sizeof (head);} // three parameters with a directed edge, four parameters without a directed edge void addEdg (int u, int v, captype c, captype rc = 0) {edg [eid]. to = v; edg [eid]. next = head [u]; edg [eid]. cap = c; edg [eid]. flow = 0; head [u] = eid ++; edg [eid]. to = u; edg [eid]. next = head [v]; edg [eid]. cap = rc; edg [eid]. flow = 0; head [v] = eid ++;} captype maxFlow_sap (int sNode, int eNode, int n) {// n indicates the total number of points including the Source and Sink points, memset (gap, 0, sizeof (gap); memset (dis, 0, sizeof (dis); memcpy (cur, head, sizeof (head )); pre [sNode] =-1; gap [0] = n; captype ans = 0; // maximum stream int u = sNode; while (dis [sNode]
    
     
Edg [I]. cap-edg [I]. flow) {Min = edg [I]. cap-edg [I]. flow; inser = I;} for (int I = pre [u]; I! =-1; I = pre [edg [I ^ 1]. to]) {edg [I]. flow + = Min; edg [I ^ 1]. flow-= Min; // flow of the reflux edge} ans + = Min; u = edg [inser ^ 1]. to; continue;} bool flag = false; // judge whether int v can flow to adjacent points from the u point; for (int I = cur [u]; I! =-1; I = edg [I]. next) {v = edg [I]. to; if (edg [I]. cap-edg [I]. flow> 0 & dis [u] = dis [v] + 1) {flag = true; cur [u] = pre [v] = I; break ;}} if (flag) {u = v; continue;} // if no adjacent vertex of the stream is found above, the distance (or height) of the starting point u is changed) + 1 int Mind = n; for (int I = head [u]; I! =-1; I = edg [I]. next) if (edg [I]. cap-edg [I]. flow> 0 & Mind> dis [edg [I]. to]) {Mind = dis [edg [I]. to]; cur [u] = I;} gap [dis [u] --; if (gap [dis [u] = 0) return ans; // when the distance of dis [u] is absent, it is impossible to find an augmented stream path from the source point. // there is only one distance between the sink point and the current point, then, from the source point to the sink point will inevitably pass through the current point. However, if the current point fails to find the point that can flow, it will inevitably cut off dis [u] = Mind + 1; // If a adjacent vertex that can be streamed is found, the distance between the adjacent vertex is + 1. If not, the distance is n + 1 gap [dis [u] ++; if (u! = SNode) u = edg [pre [u] ^ 1]. to; // return an edge} return ans;} int id [MAXN] [MAXN], vist [MAXN]; void bfs () {queue
     
      
Q; int u, v; memset (vist, 0, sizeof (vist); vist [1] = 1; q. push (1); while (! Q. empty () {u = q. front (); q. pop (); for (int I = head [u]; I! =-1; I = edg [I]. next) {v = edg [I]. to; if (! Vist [v] & edg [I]. cap-edg [I]. flow> 0) vist [v] = 1, q. push (v) ;}} int main () {int T, n, m, u, v, c; scanf (% d, & T); while (T --) {scanf (% d, & n, & m); init (); memset (id,-1, sizeof (id); while (m --) {scanf (% d, & u, & v, & c); id [u] [v] = eid; addEdg (u, v, c );} int ans = maxFlow_sap (1, n, n); bfs (); for (int I = 2; I
      
        Ans) ans = tans;} printf (% d, ans );}}
      
     
    
   
  
 


 

Related Article

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.