HDU 1853 Cyclic Tour maximum weight matches the minimum edge of all points connected to the ring and

Source: Internet
Author: User

Links: http://acm.hdu.edu.cn/showproblem.php?pid=1853

Cyclic TourTime limit:1000/1000 MS (java/others) Memory limit:32768/65535 K (java/others)
Total submission (s): 1904 Accepted Submission (s): 951


Problem Descriptionthere is N cities in our country, and M one-way roads connecting them. Now Little Tom wants to make several cyclic tours, which satisfy so, each cycle contain at least both cities, and each CI Ty belongs to one cycle exactly. Tom wants the total length of the "all" tours minimum, but he's too lazy to calculate. Can you help him?

Inputthere is several test cases in the input. You should process to the end of file (EOF).
The first line of all test case contains, integers N (n≤100) and M, indicating the number of cities and the number O F Roads. The M lines followed, each of them contains three numbers A, B, and C, indicating that there are A road from city A to city B, whose length is C. (1≤a,b≤n, A≠b, 1≤c≤1000).

Outputoutput one number for each test case, indicating the minimum length of the tours. If There is no such tours, output-1.

Sample Input
6 91 2 52 3 53 1 103 4 124 1 84 6 115 4 75 6 96 5 46 51 2 12 3 13 4 14 5 15 6 1

Sample Output
42-1Hint


Test instructions

Give you a number of points and take the right to the edge, you need to connect all points into a ring, can be multiple rings, but each ring must have at least two points.


Practice:

All points into the ring, you can know that all the points in and out of the degree of 1. And as long as this condition is met, all points must be in one ring, that is, the condition is met.


So you can build a binary graph, the left point from S inflow cost is 0, flow is 1, the degree is 1, the right.

Then according to the side of the traffic to build 1, the cost is the edge of the right side, this is the maximum weight matching diagram.

So as long as the full flow will meet the conditions.



#include <stdio.h> #include <string.h> #include <queue>using namespace std;//minimum cost maximum flow, the maximum cost only need to take the opposite number, The result is the opposite number. The total number of points is N, the number of points 0~n-1const int maxn = 10000;const int MAXM = 100000;const int INF = 0x3f3f3f3f;struct Edge{int to,next,cap, Flow,cost;} Edge[maxm];int head[maxn],tol;int pre[maxn],dis[maxn];bool vis[maxn];int n;//node total number, node number from 0~n-1void init (int N) {n = n; Tol = 0;memset (head,-1,sizeof (Head));} void Addedge (int u,int v,int cap,int cost) {edge[tol].to = V;edge[tol].cap = Cap;edge[tol].cost = Cost;edge[tol].flow = 0;e Dge[tol].next = Head[u];head[u] = tol++;edge[tol].to = U;edge[tol].cap = 0;edge[tol].cost =-cost;edge[tol].flow = 0;edge[ Tol].next = head[v];head[v] = tol++;} BOOL SPFA (int s,int t) {queue<int>q;for (int i = 0;i < n;i++) {Dis[i] = inf;vis[i] = false;pre[i] =-1;} Dis[s] = 0;vis[s] = True;q.push (s), while (!q.empty ()) {int u = q.front (); Q.pop (); Vis[u] = false;for (int i = head[u]; I! =-1 ; i = edge[i].next) {int v = edge[i].to;if (Edge[i].cap > Edge[i].flow &&d Is[v] > Dis[u] + edge[i].cost) {Dis[v] = Dis[u] + edge[i].cost;pre[v] = i;if (!vis[v]) {Vis[v] = True;q.push (v);}}} if (pre[t] = =-1) return false;else return true; The maximum flow is returned, the cost is the minimum charge int mincostmaxflow (int s,int t,int &cost) {int flow = 0;cost = 0;while (SPFA (s,t)) {int Min = Inf;f or (int i = pre[t];i! = -1;i = Pre[edge[i^1].to]) {if (min > Edge[i].cap-edge[i].flow) Min = Edge[i].cap-edge[i].flow;} for (int i = pre[t];i! = -1;i = Pre[edge[i^1].to]) {Edge[i].flow + = min;edge[i^1].flow-= min;cost + = Edge[i].cost * Min;} Flow + = Min;} return flow;} int main () {int n,m;while (scanf ("%d%d", &n,&m)!=eof) {init (2*n+2); int ss=0;int ee=2*n+1;for (int i=0;i<m;i++ {int u,v,w;scanf ("%d%d%d", &u,&v,&w); Addedge (u,v+n,1,w);} for (int i=1;i<=n;i++) {Addedge (ss,i,1,0); Addedge (i+n,ee,1,0);} int Cost,liu;liu=mincostmaxflow (ss,ee,cost); if (liu!=n) {puts ("-1");} else{printf ("%d\n", Cost);}} return 0;}




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

HDU 1853 Cyclic Tour maximum weight matches the minimum edge of all points connected to the ring and

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.