438. The Glorious Karlutka River =)

Source: Internet
Author: User

There are some floating objects in the river, each of which has a capacity. Find the shortest time for m people to cross the river.

Question concept: a typical question, with time restrictions, and the capacity on the point, needs to be split, assuming that the answer time is T, then each point should be split into 2 x T points specific method: s-> SS for the number of people, SS-> V infinite, UT, 1-> UT, 2 for the point of capacity, UT-1, 2-> VT, 1 infinite, UT-1, 2-> UT, 1 infinite, UT-1, 2-> T infinite efficiency the key is to see how to build a diagram of the solution and the answer T enumeration, I used four methods, time efficiency comparison is as follows: sap (adjacent table) traverses by time from small to large. Every time a new graph is created, the system expands by 2173 MSsap (adjacent table) for two minutes. Every time a new graph is created, the system expands by 409 MSEK (adjacent table) increase by time from small to large, but increase by 2 * n points each time and then increase by 47 MSsap (adjacent table) by time from small to large, however, after adding 2 * n points each time, the system is extended based on the original (but dis and gap initialization) by 772 MS (here sap is slower than EK, it may be because dis and gap are cleared during each push (the answer is not answered if Initialization is not performed), so the push is very slow and I don't know if there are any better algorithms. (The above ideas are taken from the Internet)
At the beginning, I thought of splitting points and adding two points. I was not sure. I still read the problem solution. I saw this method in the solution. I tried it and kept fighting, so we can only use the step-by-step method, and use the dinic template.
[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <string>
# Include <queue>
# Include <algorithm>
# Include <vector>
# Include <stack>
# Include <list>
# Include <iostream>
# Include <map>
# Include <math. h>
Using namespace std;
# Define inf 0x3f3f3f
# Deprecision Max 55555
Int max (int a, int B)
{
Return a> B? A: B;
}
Int min (int a, int B)
{
Return a <B? A: B;
}
Int x [100], y [100], c [100], m, n, d, w;
Int dis [Max], p [Max];
Int src, dest, eid, ans;
Int q [Max];
Struct node
{
Int to, c, next;
} E [1, 2222222];
Void addedge (int u, int v, int c)
{
E [eid] = (node) {v, c, p [u]};
P [u] = eid ++;
E [eid] = (node) {u, 0, p [v]};
P [v] = eid ++;
}
Int bfs ()
{
Int I, u, v, head = 0, tail = 0;
For (I = 0; I <= dest + 1; I ++) dis [I] = 0;
Dis [src] = 1;
Q [tail ++] = src;
While (head <tail)
{
U = q [head ++];
For (I = p [u]; ~ I; I = e [I]. next)
If (e [I]. c & dis [v = e [I]. to] = 0)
{
Dis [v] = dis [u] + 1;
If (v = dest)
Return 1;
Q [tail ++] = v;
}
}
Return 0;
}
Int dfs (int u, int limit)
{
If (u = dest)
Return limit;
Int v, tmp, cost = 0;
For (int I = p [u]; ~ I; I = e [I]. next)
If (e [I]. c & dis [u] + 1 = dis [v = e [I]. to])
{
Tmp = dfs (v, min (limit-cost, e [I]. c ));
If (tmp)
{
E [I]. c-= tmp;
E [I ^ 1]. c + = tmp;
Cost + = tmp;
}
Else
Dis [v] =-1;
}
Return cost;
}
Int Dinic ()
{
While (bfs ())
Ans + = dfs (src, inf );
Return ans;
}
Bool find (int u)
{
Int I;
Dis [u] = 1;
If (w-y [u] <= d)
{
Dis [n + 1] = 1;
Return true;
}
For (I = 1; I <= n; I ++)
{
If (! Dis [I] & (x [u]-x [I]) * (x [u]-x [I]) + (y [u]-y [I]) * (y [u]-y [I]) <= d * d)
{
If (find (I ))
Return true;
}
}
Return false;
}
Int main ()
{
Int I, j, k;
While (scanf ("% d", & n, & m, & d, & w )! = EOF)
{
Int cnt = 0;
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & x [I], & y [I], & c [I]);
If (c [I]> 0)
{
X [++ cnt] = x [I];
Y [cnt] = y [I];
C [cnt] = c [I];
}
}
N = cnt;
If (d> = w)
{
Printf ("1 \ n ");
Continue;
}
For (I = 0; I <= n + 1; I ++) dis [I] = 0;
For (I = 1; I <= n; I ++)
{
If (! Dis [I] & y [I] <= d)
{
If (find (I) break;
}
}
If (dis [n + 1] = 0)
{
Puts ("IMPOSSIBLE ");
Continue;
}
Memset (p,-1, sizeof (p ));
Eid = 0;
Ans = 0; www.2cto.com
Src = 0; dest = 2 * (n + m + 2) * n + 1;
For (k = 1; k <= n + m + 2; k ++)
{
For (I = 1; I <= n; I ++)
{
If (y [I] <= d)
Addedge (0, (2 * K-2) * n + I, inf );
If (w-y [I] <= d)
Addedge (2 * K-1) * n + I, dest, inf );
Addedge (2 * K-2) * n + I, (2 * K-1) * n + I, c [I]);
}
If (k> 1)
{
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
{
If (x [I]-x [j]) * (x [I]-x [j]) + (y [I]-y [j]) * (y [I]-y [j]) <= d * d & I! = J)
Addedge (2 * K-3) * n + I, (2 * K-2) * n + j, inf );
}
}
If (Dinic ()> = m) break;
}
Printf ("% d \ n", k + 1 );
}
}

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.