UVALive-2197 Paint the Roads (fee stream)

Source: Internet
Author: User

The main topic: There are n points, M edge, your task is to select some of the edges, so that each selected edge of a number of non-public side of the loop, and each city happens in the K back road, the selected edge of the total weight required minimum

Problem-solving ideas: K loops, each city has, indicating that each city's degrees and degrees are K, so to build the edge
Source points to each city, capacity is K, cost 0
Each city is connected to a meeting point with a capacity of K and costs 0
Side connects two cities with a capacity of 1, the cost is the right value

Run minimum cost maximum flow

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace STD;#define N 1010#define INF 0x3f3f3f3fstructedge{intFrom, to, caps, flow, cost; Edge () {} Edge (intFromintTo,intCapintFlowintCost): From, to, Cap (CAP), flow, cost (cost) {}};structmcmf{intN, m, source, sink, flow, cost; vector<Edge>Edges vector<int>G[n];intD[n], F[n], p[n];BOOLVis[n];voidInitintN) { This->n = n; for(inti =0; I <= N;        i++) g[i].clear ();    Edges.clear (); }voidAddedge (intFromintTo,intCapintCost) {Edges.push_back (Edge (from, to, Cap,0, cost)); Edges.push_back (Edge (to, from,0,0,-cost));        m = Edges.size (); G[from].push_back (M-2); G[to].push_back (M-1); }BOOLBellmanford (intSintTint&flow,int&cost) { for(inti =0; I <= N; i++) D[i] = INF;memset(Vis,0,sizeof(VIS)); Vis[s] =1; D[s] =0; F[s] = INF; P[s] =0; Queue<int>Q; Q.push (s); while(! Q.empty ()) {intU = Q.front ();            Q.pop (); Vis[u] =0; for(inti =0; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(E.cap > E.flow && d[e.to] > D[u] + e.cost)                    {D[e.to] = D[u] + e.cost;                    P[e.to] = G[u][i]; F[e.to] = min (F[u], e.cap-e.flow);if(!vis[e.to]) {Vis[e.to] =true;                    Q.push (e.to); }                }            }        }if(D[t] = = INF)return false;        Flow + = F[t]; Cost + = D[t];intu = t; while(U! = s)            {Edges[p[u]].flow + = f[t]; Edges[p[u] ^1].flow-= f[t];        u = edges[p[u]].from; }return true; }voidMincost (intSintT) {flow =0, cost =0; while(Bellmanford (S, t, flow, cost)); }}; MCMF MCMF;intN, M, K;voidSolve () {scanf("%d%d%d", &n, &m, &k);intSource =2* N, sink =2* n +1; Mcmf.init (sink); for(inti =0; I < n; i++) {MCMF. Addedge (Source, I *2K0); MCMF. Addedge (i *2+1, Sink, K,0); }intU, V, c; for(inti =1; I <= m; i++) {scanf("%d%d%d", &u, &v, &c); MCMF. Addedge (U *2, V *2+1,1, c); } MCMF. Mincost (source, sink);if(Mcmf.flow = = k * N)printf("%d\n", mcmf.cost);Else printf(" -1\n");}intMain () {intTestscanf("%d", &test); while(test--) solve ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVALive-2197 Paint the Roads (fee stream)

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.