POJ 2112 Optimal Milking 最優擠奶方案 Floyd演算法+二分尋找+最大流,pojmilking

來源:互聯網
上載者:User

POJ 2112 Optimal Milking 最優擠奶方案 Floyd演算法+二分尋找+最大流,pojmilking

題目連結:POJ 2112 Optimal Milking

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

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 symmetric 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

題意:

K個擠奶器(編號1~K),每個每天最多擠M頭奶牛。共有C頭奶牛(編號K+1~K+C)。擠奶器和奶牛間有不同長度的路。

編寫程式,尋找一個方案,安排每頭牛到某個擠奶器擠奶,並使得C頭奶牛需要走的所有路程的最大路程最小。

分析:

因為有多個擠奶器和多頭奶牛,因而要求最短路需要Floyd。然後用最大流演算法求解;搜尋最大距離的最小值採用二分枚舉。

關鍵問題是構圖:

構圖思路:

(1)以0為源點s,n+1為匯點t。

(2)s向每頭奶牛引一條邊,容量為1,代表s走向每頭奶牛的最大奶牛數。

(3)每個擠奶器向t引一條邊,容量為M,代表每個擠奶器最多向t走M頭奶牛。

(4)對於每頭奶牛通向每個擠奶器的最短路,如果小於枚舉當前距離,說明能夠走到,那麼從奶牛到擠奶器引一條邊,容量為1。

(5)構圖完畢。

最大流:

最大流可以用三種方法實現:

(1)EK,普通的SAP演算法,因要不斷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找增廣路徑        {            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])  //改進網路流        {            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)    {        int 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%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;}
(2)Dinic,SAP的一種最佳化方案,儲存層次網路,用DFS增廣更加快速。

#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,改進SAP,更加最佳化,活用層次網路,只需要一遍BFS,然後進行增廣。

#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;}

時間比較:

前兩個分別為EK和ISAP演算法,後面的為Dinic。不知為何我寫的ISAP時間要比Dinic總是慢那麼一點點,不過能順利過題目就好了。

可以看出EK效率多差了吧:

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


試問Floyd演算法可否處理有向圖?

當然可以了
只要不是帶負環(這樣的話就沒有正確答案了) 都可以處理的
稠密圖上效率比較好
 
演算法設計比賽做什演算法好

應該是ACM吧
就是給你8-10道演算法題目,5個小時,做出來多的題目數越多,排名越靠前,如果題目數一樣多的看用的時間。
時間的計算方法如下:
例如你A題用了20分鐘AC,然後B題有用了30分鐘AC(此時是比賽開始50分鐘),又用了30分鐘AC了C題,那麼你的時間(penalty )是
20 + 50 + 80 = 150分鐘

比賽中常用的演算法有
1。動態規劃
2。搜尋
3。貪心
4。圖論
5。組合數學
6。計算幾何
7。數論
等

推薦到
acm.pku.edu.cn
acm.zju.edu.cn
acm.hdu.edu.cn
acm.timus.ru
這幾個OJ上練習

比較好的題目分類(POJ上的)
1。這個是我最喜歡的
初期:
一.基本演算法:
(1)枚舉. (poj1753,poj2965)(2008-10-27Done 位元運算+寬搜)
(2)貪心(poj1328,poj2109,poj2586)
(3)遞迴和分治法.
(4)遞推.
(5)構造法.(poj3295)
(6)類比法.(poj1068,poj2632,poj1573,poj2993,poj2996)
二.圖演算法:
(1)圖的深度優先遍曆和廣度優先遍曆.
(2)最短路徑演算法(dijkstra,bellman-ford,floyd,heap+dijkstra)(2008-08-29Done)
(poj1860,poj3259,poj1062,poj2253,poj1125,poj2240)
(3)最小產生樹演算法(prim,kruskal)
(poj1789,poj2485,poj1258,poj3026)
(4)拓撲排序 (poj1094)(2008-09-01Done)
(5)二分圖的最大匹配 (匈牙利演算法) (poj3041,poj3020)
(6)最大流的增廣路演算法(KM演算法). (poj1459,poj3436)
三.資料結構.
(1)串 (poj1035,poj3080,poj1936)
(2)排序(快排、歸併排(與逆序數有關)、堆排) (poj2388,poj2299)
(3)簡單並查集的應用.
(4)雜湊表和二分尋找等高效尋找法(數的Hash,串的Hash)
(poj3349,poj3274,POJ2151,poj1840,poj2002,poj2503)
(5)哈夫曼樹(poj3253)(2008-09-02Done)
(6)堆
(7)trie樹(靜態建樹、動態建樹) (poj2513)(2008-10-23Done 並查集、歐拉)
四.簡單搜尋
(1)深度優先搜尋 (poj2488,poj3083,poj3009,poj1321,poj2251)
(2)廣度優先搜尋(poj3278,poj1426,......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.