POJ 2112 Optimal Milking scheme Floyd algorithm + Binary Search + maximum stream, pojmilking

Source: Internet
Author: User

POJ 2112 Optimal Milking scheme Floyd algorithm + Binary Search + maximum stream, pojmilking

Link: POJ 2112 Optimal Milking

Optimal Milking
Time Limit:2000 MS   Memory Limit:30000 K
Total Submissions:12446   Accepted:4494
Case Time Limit:1000 MS

Description

FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. the milking machine locations are named by ID numbers 1 .. k; the cow locations are named by ID numbers K + 1 .. K + C.

Each milking point can "process" at most M (1 <= M <= 15) cows each day.

Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized ). at least one legal assignment is possible for all input data sets. cows can traverse several paths on the way to their milking machine.

Input

* Line 1: A single line with three space-separated integers: K, C, and M.

* Lines 2 .....: Each of these K + C lines of K + C space-separated integers describes the distances between pairs of various entities. the input forms a mathematical matrix. line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. distances of entities directly connected by a path are positive integers no larger than 200. entities not directly connected by a path have a distance of 0. the distance from an entity to itself (I. e ., all numbers on the diagonal) is also given as 0. to keep the input lines of reasonable length, when K + C> 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. each new row begins on its own line.

Output

A single line with a single integer that is the minimum possible total distance for the furthest walking cow.

Sample Input

2 3 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0

Sample Output

2

Source

USACO 2003 u s Open

Question:

1 ~ K), a maximum of M cows can be squeezed every day. A total of C cows (numbered K + 1 ~ K + C ). There is a different length between a milk extruder and a cow.

Write a program, find a solution, arrange each cow to a milking machine, and make C cows need to take the maximum distance of all.

Analysis:

Because there are multiple milk runners and multiple cows, Floyd is required for the shortest. Then use the maximum stream algorithm to solve the problem. The minimum value of the maximum search distance is classified into two enumeration points.

The key issue is the diagram:

Architecture:

(1) Take 0 as the Source Vertex s, and n + 1 as the sink vertex t.

(2) s leads one edge to each cow. The capacity is 1, which indicates the maximum number of cows in every cow.

(3) Each milking machine leads a side to t and the capacity is M, which means that each milking machine can walk at most M cows to t.

(4) For the shortest short circuit from each cow to each milking machine, if it is less than the current enumeration distance, it indicates that it can be reached, then from the cow to the milking machine leads one side, the capacity is 1.

(5) The composition is complete.

Maximum stream:

The maximum stream can be implemented in three ways:

(1) EK, a common SAP algorithm, with high complexity due to continuous BFS:

# Include <iostream >#include <cstdio> # include <cstring> # include <queue> # include <vector> using namespace std; # define maxn 1010 # define INF 0x3f3f3f3fint s, t, n, K, C, M; int a [maxn], pre [maxn]; int flow [maxn] [maxn]; int cap [maxn] [maxn], mp [maxn] [maxn]; int EK () {queue <int> q; memset (flow, 0, sizeof (flow); int ans = 0; while (1) {memset (a, 0, sizeof (a); a [s] = INF; q. push (s); while (! Q. empty () // bfs find the augmented path {int u = q. front (); q. pop (); for (int v = 1; v <= n; v ++) if (! A [v] & cap [u] [v]> flow [u] [v]) {pre [v] = u; q. push (v); a [v] = min (a [u], cap [u] [v]-flow [u] [v]);} if (a [t] = 0) break; for (int u = t; u! = S; u = pre [u]) // improved network stream {flow [pre [u] [u] + = a [t]; flow [u] [pre [u]-= a [t];} ans + = a [t];} return ans;} void Floyd () {for (int k = 1; k <= n; k ++) for (int I = 1; I <= n; I ++) {if (mp [I] [k]! = INF) for (int j = 1; j <= n; j ++) mp [I] [j] = min (mp [I] [j], mp [I] [k] + mp [k] [j]) ;}} void build (int mid) {n = K + C; memset (cap, 0, sizeof (cap); for (int I = K + 1; I <= n; I ++) cap [s] [I] = 1; for (int I = 1; I <= K; I ++) cap [I] [t] = M; for (int I = K + 1; I <= n; I ++) for (int j = 1; j <= K; j ++) if (mp [I] [j] <= mid) cap [I] [j] = 1; n = K + C + 2;} void Solve () {s = 0, t = n + 1; int l = 0, r = 10000; while (l <r) {I Nt mid = (l + r)/2; build (mid); int ans = EK (); if (ans> = C) r = mid; else l = mid + 1;} printf ("% d \ n", r);} int main () {// freopen ("poj_2112.txt", "r ", stdin); while (~ Scanf ("% d", & K, & C, & M) {n = K + C; for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) {scanf ("% d", & mp [I] [j]); if (mp [I] [j] = 0) mp [I] [j] = INF;} Floyd (); Solve ();} return 0 ;}
(2) An optimization solution of Dinic and SAP, which stores hierarchical networks and uses DFS to increase the size more quickly.

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;#define maxn 250#define INF 0x3f3f3f3fstruct Edge{    int from, to, cap;};vector<Edge> EG;vector<int> G[maxn];int n, s, t, d[maxn], cur[maxn], mp[maxn][maxn];int K, C, M;void addEdge(int from, int to, int cap){    EG.push_back((Edge){from, to, cap});    EG.push_back((Edge){to, from, 0});    int x = EG.size();    G[from].push_back(x-2);    G[to].push_back(x-1);}bool bfs(){    memset(d, -1, sizeof(d));    queue<int> q;    q.push(s);    d[s] = 0;    while(!q.empty())    {        int x = q.front();        q.pop();        for(int i = 0; i < G[x].size(); i++)        {            Edge& e = EG[G[x][i]];            if(d[e.to] == -1 && e.cap > 0)            {                d[e.to] = d[x]+1;                q.push(e.to);            }        }    }    return (d[t]!=-1);}int dfs(int x, int a){    if(x == t || a == 0) return a;    int flow = 0, f;    for(int& i = cur[x]; i < G[x].size(); i++)    {        Edge& e = EG[G[x][i]];        if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(a, e.cap))) > 0)        {            e.cap -= f;            EG[G[x][i]^1].cap += f;            flow += f;            a -= f;            if(a == 0) break;        }    }    return flow;}int Dinic(){    int ans = 0;    while(bfs())    {        memset(cur, 0, sizeof(cur));        ans += dfs(s, INF);    }    EG.clear();    for(int i = 0; i < n; ++i)        G[i].clear();    return ans;}void Floyd(){    for(int k = 1; k <= n; k++)        for(int i = 1; i <= n; i++) {            if(mp[i][k] != INF)                for(int j = 1; j <= n; j++)                    mp[i][j] = min(mp[i][j], mp[i][k]+mp[k][j]);        }}void build(int mid){    n = K+C;    for(int i = K+1; i <= n; i++)        addEdge(s, i, 1);    for(int i = 1; i <= K; i++)        addEdge(i, t, M);    for(int i = K+1; i <= n; i++)        for(int j = 1; j <= K; j++)            if(mp[i][j] <= mid)                addEdge(i, j, 1);    n = K+C+2;}void Solve(){    s = 0, t = n+1;    int l = 0, r = 10000;    while(l < r)    {        int mid = (l+r)/2;        build(mid);        int ans = Dinic();        if(ans >= C) r = mid;        else l = mid+1;    }    printf("%d\n", r);}int main(){    //freopen("poj_2112.txt", "r", stdin);    while(~scanf("%d%d%d", &K, &C, &M)) {        n = K+C;        for(int i = 1; i <= n; i++)            for(int j = 1; j <= n; j++) {                scanf("%d", &mp[i][j]);                if(mp[i][j] == 0) mp[i][j] = INF;            }    Floyd();    Solve();    }    return 0;}
(3) ISAP, improving SAP, making it more optimized and using hierarchical networks. It only needs to be BFS once and then extended.

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;#define maxn 250#define INF 0x3f3f3f3fstruct Edge{    int from, to, cap, flow;};vector<Edge> EG;vector<int> G[maxn];int n, s, t, d[maxn], cur[maxn], p[maxn], num[maxn], mp[maxn][maxn];bool vis[maxn];int K, C, M;void addEdge(int from, int to, int cap){    EG.push_back((Edge){from, to, cap, 0});    EG.push_back((Edge){to, from, 0, 0});    int x = EG.size();    G[from].push_back(x-2);    G[to].push_back(x-1);}void bfs(){    memset(vis, false, sizeof(vis));    queue<int> q;    vis[t] = true;    d[t] = 0;    q.push(t);    while(!q.empty()) {        int x = q.front();        q.pop();        for(int i = 0; i < G[x].size(); i++) {            Edge& e = EG[G[x][i]^1];            if(!vis[e.from] && e.cap > e.flow) {                vis[e.from] = true;                d[e.from] = d[x]+1;                q.push(e.from);            }        }    }}int augment(){    int x = t, a = INF;    while(x != s) {        Edge& e = EG[p[x]];        a = min(a, e.cap-e.flow);        x = EG[p[x]].from;    }    x = t;    while(x != s) {        EG[p[x]].flow += a;        EG[p[x]^1].flow -= a;        x = EG[p[x]].from;    }    return a;}int ISAP(){    int ans =0;    bfs();    memset(num, 0, sizeof(num));    for(int i = 0; i < n; i++)        num[d[i]]++;    int x = s;    memset(cur, 0, sizeof(cur));    while(d[s] < n) {        if(x == t) {            ans += augment();            x = s;        }        bool flag = false;        for(int i = cur[x]; i < G[x].size(); i++) {            Edge& e = EG[G[x][i]];            if(e.cap > e.flow && d[x] == d[e.to]+1) {                flag = true;                p[e.to] = G[x][i];                cur[x] = i;                x = e.to;                break;            }        }        if(!flag) {            int m = n-1;            for(int i = 0; i < G[x].size(); i++) {                Edge& e = EG[G[x][i]];                if(e.cap > e.flow)                    m = min(m, d[e.to]);            }            if(--num[d[x]] == 0) break;            num[d[x] = m+1]++;            cur[x] = 0;            if(x != s)                x = EG[p[x]].from;        }    }    EG.clear();    for(int i = 0; i < n; ++i)        G[i].clear();    return ans;}void Floyd(){    for(int k = 1; k <= n; k++)        for(int i = 1; i <= n; i++) {            if(mp[i][k] != INF)                for(int j = 1; j <= n; j++)                    mp[i][j] = min(mp[i][j], mp[i][k]+mp[k][j]);        }}void build(int mid){    n = K+C;    for(int i = K+1; i <= n; i++)        addEdge(s, i, 1);    for(int i = 1; i <= K; i++)        addEdge(i, t, M);    for(int i = K+1; i <= n; i++)        for(int j = 1; j <= K; j++)            if(mp[i][j] <= mid)                addEdge(i, j, 1);    n = K+C+2;}void Solve(){    s = 0, t = n+1;    int l = 0, r = 10000;    while(l < r)    {        int mid = (l+r)/2;        build(mid);        int ans = ISAP();        if(ans >= C) r = mid;        else l = mid+1;    }    printf("%d\n", r);}int main(){    //freopen("poj_2112.txt", "r", stdin);    while(~scanf("%d%d%d", &K, &C, &M)) {        n = K+C;        for(int i = 1; i <= n; i++)            for(int j = 1; j <= n; j++) {                scanf("%d", &mp[i][j]);                if(mp[i][j] == 0) mp[i][j] = INF;            }    Floyd();    Solve();    }    return 0;}

Time Comparison:

The first two algorithms are EK and ISAP, respectively, followed by Dinic. I don't know why I write ISAP time is a little slower than that of Dinic, but it is better to pass the problem smoothly.

It can be seen that the EK efficiency is much worse:

Run ID User Problem Result Memory Time Language Code Length Submit Time
13475128 Xxx 2112 Accepted 9584 K 1875 MS G ++ 2266B 02:39:13
13475120 Xxx 2112 Accepted 1328 K 157 MS G ++ 3678B 02:35:15
13475117 Xxx 2112 Accepted 1236 K 125 MS G ++ 2812B 02:33:48
13475113 Xxx 2112 Accepted 1236 K 141 MS G ++ 2814B 02:33:09
13475102 Xxx 2112 Accepted 1236 K 94 MS G ++ 2814B 02:26:43


Can the Floyd algorithm process directed graphs?

Of course.
As long as it is not a negative ring (in this case, there is no correct answer), it can be processed.
High Efficiency in dense Graphs

What algorithms are good for algorithm design competitions?

It should be ACM.
It is 8-10 algorithm questions for you. The more questions you have made in five hours, the higher the ranking. If the number of questions is the same, the more time you use.
The time calculation method is as follows:
For example, if you use question A for 20 minutes, then question B uses Question A for 30 minutes (this is the start of the game for 50 minutes) and question C for 30 minutes, so your time (penalty) is
20 + 50 + 80 = 150 minutes

Common algorithms used in competitions include:
1. Dynamic Planning
2. Search
3. Greedy
4. Graph Theory
5. Combined mathematics
6. Computational ry
7. Number Theory
And so on

Recommended
Acm.pku.edu.cn
Acm.zju.edu.cn
Acm.hdu.edu.cn
Acm.timus.ru
These OJ exercises

Better question classification (on POJ)
1. This is my favorite
Initial stage:
I. Basic Algorithms:
(1) enumeration. (poj1753, poj2965) (2008-10-27Done bitwise operation + wide search)
(2) greedy (poj1328, poj2109, poj2586)
(3) recursion and divide and conquer.
(4) recurrence.
(5) constructor. (poj3295)
(6) simulation method. (poj1068, poj2632, poj1573, poj2993, poj2996)
Ii. Graph Algorithm:
(1) depth first traversal and breadth first traversal.
(2) shortest path algorithm (dijkstra, bellman-ford, floyd, heap + dijkstra) (2008-08-29Done)
(Poj1860, poj3259, poj1062, poj2253, poj1125, poj2240)
(3) Minimum Spanning Tree Algorithm (prim, kruskal)
(Poj1789, poj2485, poj1258, poj3026)
(4) Topology Sorting (poj1094) (2008-09-01Done)
(5) maximum matching of bipartite graphs (Hungary algorithm) (poj3041, poj3020)
(6) augmented Path Algorithm for the maximum stream (KM algorithm). (poj1459, poj3436)
Iii. data structure.
(1) string (poj1035, poj3080, poj1936)
(2) sorting (fast sorting, Merge Sorting (related to the number of reverse orders), heap sorting) (poj2388, poj2299)
(3) Simple and query set applications.
(4) efficient search methods such as Hash table and binary search (number Hash, string Hash)
(Poj3349, poj3274, POJ2151, poj1840, poj2002, poj2503)
(5) Harman tree (poj3253) (2008-09-02Done)
(6) Heap
(7) trie tree (static and dynamic) (poj2513) (done, query set, Euler)
4. Simple search
(1) Deep Priority Search (poj2488, poj3083, poj3009, poj1321, poj2.pdf)
(2) breadth-first search (poj3278, poj1426, ...... the remaining full text>
 

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.