UVA 10746Crime wave-the Sequel "minimum charge maximum Flow"

Source: Internet
Author: User

Topic Links:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 1687

Test instructions: give you the distance from the N city to the M seaport, and ask for the shortest average sailing distance for each city.

The source point to the city to build the city to the seaport seaport to the meeting point capacity of 1, the last city to the seaport of the cost of distance

Code:

#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <queue>using namespace STD;//************************************************************//Minimum cost maximum flow algorithm//SPFA to find the shortest way//adjacency matrix form//Initialize: Cap: Capacity, no edge for 0//cost: Cost, symmetrical form, 0 without side//c is the minimum cost//f is the maximum flow//*******************************************************Const intMAXN = -;Const DoubleINF =0x3fffffff;DoubleCAP[MAXN][MAXN];//capacity, No side for 0DoubleFLOW[MAXN][MAXN];//cost matrix is symmetrical, there is a charge of I to J, then the cost of J to I is the opposite numberDoubleCOST[MAXN][MAXN];//CostintN///vertex number 0~n-1DoubleF//Maximum flowDoubleC//Minimum costintStart, End;//Source point and meeting pointBOOLVIS[MAXN];//In the queue flagintQUE[MAXN];intPRE[MAXN];DoubleDIST[MAXN];//s-t path Minimum costBOOLSPFA () {intFront =0, rear =0; for(intU =0; U <= N; u++) {if(U = = start)            {que[rear++] = u; Dist[u] =0; Vis[u] =true; }Else{Dist[u] = INF; Vis[u] =false; }    } while(Front! = rear) {intU = que[front++]; Vis[u] =false;if(Front >= maxn) front =0; for(intv =0; V <= N; v++) {if(Cap[u][v]>flow[u][v] && Dist[v]>dist[u] + cost[u][v])                {Dist[v] = Dist[u] + cost[u][v]; PRE[V] = u;if(!vis[v]) {Vis[v] =true; que[rear++] = v;if(Rear >= MAXN) rear =0; }            }        }    }if(Dist[end] >= INF)return false;return true;}voidMincostmaxflow () {memset(Flow,0,sizeof(flow));memset(Pre,-1,sizeof(pre));memset(Dist,0,sizeof(Dist)); c = f =0; while(SPFA ()) {DoubleMin = INF; for(intu = End; U! = start; u = pre[u]) min = min (min, cap[pre[u]][u]-flow[pre[u]][u]); for(intu = End; U! = start;            u = pre[u]) {Flow[pre[u]][u] + = Min;        Flow[u][pre[u]] = Min;        } c + = dist[end] * Min;    f + = Min; }}//************************************************************intMain () {intN, M, K; while(~scanf("%d%d", &n, &m)) {if(N = =0&& M = =0) Break;memset(Cap,0,sizeof(CAP));memset(Cost,0,sizeof(cost));memset(Vis,false,sizeof(VIS)); Start =0; n = n + M +2; End = M + N +1;Doubletmp for(inti =1; I <= N; i++) cap[0][i] =1; for(inti =1; I <= M; i++) Cap[i + n][n + M +1] =1; for(inti =1; I <= N; i++) { for(intj =1; J <= M; J + +) {scanf("%LF", &tmp);                Cost[i][j + N] = tmp;                Cost[j + n][i] =-tmp; Cap[i][j + N] =1; }} mincostmaxflow ();printf("%.2lf\n", c/n +0.001); }return 0;}

Copyright NOTICE: Reprint please indicate the source.

UVA 10746Crime wave-the Sequel "minimum charge maximum Flow"

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.