HDU 1853 Cyclic tour//fee flow

Source: Internet
Author: User
Tags min prev

Cyclic Tour Time limit:1000/1000 MS (java/others) Memory limit:32768/65535 K (java/others)
Total submission (s): Accepted submission (s): 50


Problem Description There 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?
Input there 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).
Output output one number for each test case, indicating the minimum length of the tours. If There is no such tours, output-1.
Sample Input

6 9 1 2 5 2 3 5 3 1 10 3 4 12 4 1 8 4 6 11 5 4 7 5 6 9 6 5 4 6 5 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1
Sample Output
42-1 Hint in the first sample, there be, cycles, (1->2->3->1) and (6->5->4->6) whose length is 20 + 22 = 42.
Author Roba@tju
Source HDU Programming Contest-final
Recommend LCY really at first didn't think this topic can use the cost flow to do ah .... After reading Daniel's solution to think of a few very important conditions 1. Each point belongs to only one loop, and all points must belong to the Loop 2. Each circuit must have more than two points 3. If the total cost of the circuit is minimal these conditions limit the solution to this problem can be km, in order to let the KM algorithm more familiar with a little, Tomorrow will be the km of the problem-solving report on the minimum cost flow of the composition of the first is to set up two super-source point s,t s to each point edge, the capacity is 1, the cost is 0, the limit is the penetration of each point T and each i+n edge, the capacity is 1, the cost is 0, the limit is the degree of each point, so that satisfies the first condition , that is, when the maximum flow is n, it is possible to construct such a circuit and then the cost is relatively simple, because the cost is on each side, so when there is an edge between two points, the capacity of the construction edge is 1, the cost is the topic given, do not forget to give the reverse side #include <cstdio> # include<cstring> #include <queue> #define MIN (A, b) a>b?b:a using namespace std; const int MAX = 110; const int INF = 0x7fffffff; int N, M, K; int SS, TT; int mm[2 * max][2 * MAX]; Flow map int cc[2 * max][2 * MAX]; Cost map int prev[2 * max];//The following 4 are required int flow[2 * MAX]; int dis[2 * MAX]; int in[2 * MAX]; int map[110][110]; The main changes to these two functions are void ready () {    memset (map,0,sizeof (map)),     for (int i=1;i<=m;i++ )     {        int x,y,c,         scanf ("%d%d%d", &x , &y,&c);      &NBsp  if (map[x][y]==0) map[x][y]=c;         else map[x][y]=min (map[x][y],c);    &nbsp}} void Makemap () {    ss = 0, TT = n + n + 1;     memset (mm, 0, sizeof (mm));     for (int i = 1; I <= N; i++)         mm[ss][i] = 1;//limit in and out, think about the limitations of the topic  & nbsp  for (int i = 1+n; I <= n+n; i++)        &NBSP;MM[I][TT] = 1;     for (int i = 1; I <= n; i++)         for (int j = 1; J <= N; j + +) &NBSP;&N Bsp      if (map[i][j]!=0)            &NBSP;MM[I][J + N] = 1; Make the cost map     memset (cc, 0, sizeof (CC));     for (int i = 1; I <= n; i++)         for (int j = 1; J <= N; j + +) &NBSP;&N Bsp      if (Map[i][j])         {            cc[i][ J + N] = Map[i][j];            &NBSP;CC[J + n][i] =-map[i][j];             //construction fee flow negative right side         }} int Spfa () {    memset (in, 0, sizeof (in));     memset (Dis,-1, sizeof (DIS));     memset (prev,-1, sizeof (prev));     memset (flow, 0, sizeof (flow));     in[ss] = 1, Dis[ss] = 0, flow[ss] = INF;     queue<int> Q;     q.push (SS);     while (!q.empty ())     {        int u = Q.front ();  &nbsp ;      q.pop ();         in[u] = 0;         for (int j = SS; J <= TT; j + +)         {     & nbsp      if (Mm[u][j] > 0 && (dis[j] = = 1 | | dis[u] + CC[U][J] < dis[j]))      &NB Sp      {&Nbsp;              &NBSP;DIS[J] = Dis[u] + cc[u][j];                &NBSP;//IN[J] = 1;                &NBSP;PREV[J] = u;                 flow[j] = min (Flow[u], mm[u][j]);                 if (in[j] = = 0)  in[j] = 1, Q.push (j);             }         }     }    &nbs P;return FLOW[TT]; } int MCMF () {    int res = 0,flow=0;     while (1)     {        int now = SPFA ();         if (now = = 0)  break;         int u = TT;         flow+=now;         while (U! = SS)         {         &NBSP ;  int v = prev[u];             mm[v][u] = now;            &NBSP;MM[U][V] + + now;             res + = now * Cc[v][u];             u = v;         }     }     //printf ("flow=%d/n", flow);     if (flow==n) return res;     else return-1; } int main () {    while (scanf ("%d%d", &n, &m)!=eof)     {     &NBSP ;  ready ();         makemap ();         int T=MCMF ();         printf ("%d/n", t);     }     return 0; }

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.