HDU 3526 min. cut

Source: Internet
Author: User

Click to open link

Test instructions: There are two companies to provide the n parts, each part of the price given, now to buy n parts, I can choose any of the two companies, but for the m relationship below, if I and J no longer the same company, then add the cost of C, ask to buy n parts of the minimum cost

Idea: Just read the question when it feels like the minimum cost flow, flow is 5 Bai, and then according to the relationship to build a picture, draw a will also draw not come out, looked at a moment is the smallest cut, thought to be very simple, but the idea is not the words are difficult to think of, the map is the source point, the middle of a column n parts, and then meeting Point, The source point is a company's n items, and then the middle of the meeting point is the B company's n items price, the middle of the relationship between the point of connection, then if all select a company, the smallest cut is the result, all Select B Company's minimum cut is also the result, and if there is not all in a company, For test instructions, if the B Company's 3rd items and a company's goods conflict, then add this value, in the smallest cut is to cut this edge to make the diagram is not connected, one will continue to think about the cost flow

#include <queue> #include <vector> #include <stdio.h> #include <stdlib.h> #include <string.h > #include <iostream> #include <algorithm>using namespace std;typedef long long ll;typedef unsigned long    Long Ull;const int inf=0x3f3f3f3f;const ll inf=0x3f3f3f3f3f3f3f3fll;const int maxn=1010;struct edge{int to,cap,rev; Edge (int a,int b,int c) {to=a;cap=b;rev=c;}}; Vector<edge>g[maxn];int level[maxn],iter[maxn];void add_edge (int from,int to,int cap) {G[from].push_back (Edge (    To,cap,g[to].size ())); G[to].push_back (Edge (From,0,g[from].size ()-1));}    void BFs (int s) {memset (level,-1,sizeof (level));    queue<int>que;level[s]=0;    Que.push (s);        while (!que.empty ()) {int V=que.front (); Que.pop ();            for (unsigned int i=0;i<g[v].size (); i++) {Edge &e=G[v][i];                if (e.cap>0&&level[e.to]<0) {level[e.to]=level[v]+1;            Que.push (e.to); }}}}int dfs (int V,int T,int f) {if (v==t) return F;        for (int &i=iter[v];i<g[v].size (); i++) {Edge &e=G[v][i];            if (e.cap>0&&level[v]<level[e.to]) {int D=dfs (e.to,t,min (F,e.cap));                if (d>0) {e.cap-=d;                G[e.to][e.rev].cap+=d;            return D; }}} return 0;}    int max_flow (int s,int t) {int flow=0;        while (1) {BFS (s);        if (level[t]<0) return flow;        memset (Iter,0,sizeof (ITER));        int F;    while ((F=dfs (S,t,inf)) >0) flow+=f;    }}int Main () {int n,m,cost,u,v;        while (scanf ("%d%d", &n,&m)!=-1) {for (int i=0;i<maxn;i++) g[i].clear ();            for (int i=1;i<=n;i++) {scanf ("%d", &u);        Add_edge (0,i,u);            } for (int i=1;i<=n;i++) {scanf ("%d", &u);        Add_edge (I,n+1,u);            } for (int i=0;i<m;i++) {scanf ("%d%d%d", &u,&v,&cost); Add_edge (U,v,cost);        Add_edge (V,u,cost);        } int Ans=max_flow (0,n+1);    printf ("%d\n", ans); } return 0;}

HDU 3526 min. cut

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.