POJ 1459 Power Network (multi-source/sink max flow problem)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1459

The topic gives you a big paragraph of explanation, is actually the nonsense. It is misleading to give an explanatory picture.

The main idea: for a grid, there are power stations, electricity, and transmission lines. The power station is limited, the power supply is limited, the transmission line is limited, so obviously a network flow problem. First give the line and the limit, then give the electricity side, and finally out of the power station.

Because it is a multi-source point (multiple power stations), a multi-meeting point (multiple power consumption), so the need for super-source processing.

As a super-source, it is assumed that there is a source, connected to all the source point (Power station), its line capacity is the limit of the power station, then the power station can be treated as a normal point. Assuming a super meeting point, then we can connect all the meeting point (electricity) to this super meeting point, its line capacity is the power-side limit, then, it becomes a simple single source point, single sink point maximum flow problem. It can be solved with dinic.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue > #include <algorithm> #define MAX 999999using namespace Std;int map_[200][200];int dis[200];int bfs (int s,int t)    {int now;    memset (dis,-1,sizeof (dis));    Dis[s] = 0;    Queue<int> que;    Que.push (s);        while (!que.empty ()) {now = Que.front ();        Que.pop (); for (int i = 0;i <= t;i++) if (dis[i] = = 1 && map_[now][i] > 0) {dis[                I] = Dis[now] + 1;            Que.push (i);    }} if (Dis[t]! =-1) return 1; return 0;}    int dinic (int s,int t,int x) {if (s = = t) return x;    int tmp = x; for (int i = 0;i <= t;i++) {if (dis[i] = = Dis[s] + 1 && map_[s][i] > 0) {int i            min = Dinic (i,t,min (map_[s][i],x));            Map_[s][i]-= imin;            Map_[i][s] + = Imin;        X-= Imin; }} return tmp -X;}    int main () {int n,np,nc,m;        while (~SCANF ("%d%d%d%d", &n,&np,&nc,&m)) {int i,k;        int u,v,c;        memset (map_,0,sizeof (MAP_));            for (i = 0;i < m;i++) {scanf ("(%d,%d)%d", &u,&v,&c);    Map_[u + 1][v + 1] + = C;            0 is the super source point, the other point is moved back} for (i = 0;i < np;i++) {scanf ("(%d)%d", &v,&c);        Map_[0][v + 1] + = C;            } for (i = 0;i < nc;i++) {scanf ("(%d)%d", &u,&c);        Map_[u + 1][n + 1] + = C;        } int ans = 0;        while (BFS (0,n + 1)) ans + = Dinic (0,n + 1,max);    printf ("%d\n", ans); } return 0;}

do not forget to beginner's mind, the party must always

POJ 1459 Power Network (multi-source/sink max flow problem)

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.