Poj 2135 farm tour

Source: Internet
Author: User
Farm tourtime limit: 1000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 2135
64-bit integer Io format: % LLD Java class name: Main when FJ's friends visit him on the farm, he likes to show them around. his farm comprises N (1 <= n <= 1000) fields numbered 1 .. n, the first of which contains his house and the nth of which contains the big barn. A total m (1 <= m <= 10000) paths that connect the fields in various ways. each path connects two different fields and has a nonzero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. later, he returns (potentially through some fields) back to his house again.

He wants his tour to be as short as possible, however he doesn' t want to walk on any given path more than once. calculate the shortest tour possible. FJ is sure that some tour exists for any given farm. input * Line 1: two space-separated integers: N and M.

* Lines 2.. m + 1: three space-separated integers that define a path: the starting field, the end field, and the path's length.
Outputa single line containing the length of the shortest tour.
Sample Input
4 51 2 12 3 13 4 11 3 22 4 2
Sample output
6
Sourceusaco 2003 February Green: minimum cost, maximum flow. Modeling: Find the shortest circuit of return. Each route can only be accessed once. Since we have to go back once, we have to do it again without repeating it. The path length is regarded as a fee. The capacity of each route is 1. (The cost from super source to 1 is 0, and the capacity is 2. The cost from the end to super sink is also 0, and the capacity is 2.) To set the capacity to 1. Do you still remember the zengguang road on Daming Lake? Isn't an algorithm for network stream looking for augmented channels? You can find an augmented path from the Super source point to the super sink point each time. Then, the traffic on the road is reduced by 1. Because all traffic is 1, the minimum traffic is 1. It indicates an augmented path. The remaining value is 0. It indicates that this path has been accessed. This ensures that you do not need to repeat the path. Then I will find another augmented path? Again? Because the traffic from the Super source point to 1 is less than 1, and 1 is left. Find the second augmented path. The super source cannot reach 1, that is, it cannot reach the super sink. At this time, the fee is calculated and the minimum round-trip fee officially required. We changed the process of going back to the form of the largest stream with the minimum cost to the two paths from the Super source point to the super sink point. (The Edge does not have an intersection. Although the edge from the Super source point to 1 is repeated, the cost is 0, which is the same as that of the original source point .) Note that this question is an undirected graph, so you need to create more edges.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 100100;18 struct arc{19     int u,v,w,f,next;20     arc(int a = 0,int b = 0,int c = 0,int x = 0,int y = 0){21         u = a;22         v = b;23         w = c;24         f = x;25         next = y;26     }27 };28 arc e[maxn];29 int head[1010],tot,d[1010],n,m,S,T;30 int p[1010],q[maxn<<2],hd,tail;31 bool in[1010];32 void add(int u,int v,int w,int f){33     e[tot] = arc(u,v,w,f,head[u]);34     head[u] = tot++;35     e[tot] = arc(v,u,-w,0,head[v]);36     head[v] = tot++;37 }38 bool spfa(){39     for(int i = S; i <= T; i++){40         d[i] = INF;41         in[i] = false;42         p[i] = -1;43     }44     hd = tail = 0;45     in[S] = true;46     d[S] = 0;47     q[tail++] = S;48     while(hd < tail){49         int u = q[hd++];50         in[u] = false;51         for(int i = head[u]; ~i; i = e[i].next){52             if(e[i].f > 0 && d[e[i].v] > d[u]+e[i].w){53                 d[e[i].v] = d[u] + e[i].w;54                 p[e[i].v] = i;55                 if(!in[e[i].v]){56                     in[e[i].v] = true;57                     q[tail++] = e[i].v;58                 }59             }60         }61     }62     return p[T] > -1;63 }64 int solve(){65     int ans = 0;66     while(spfa()){67         int maxVal = INF;68         for(int i = p[T]; ~i; i = p[e[i].u])69             maxVal = min(maxVal,e[i].f);70         for(int i = p[T]; ~i; i = p[e[i].u]){71             e[i].f -= maxVal;72             e[i+1].f += maxVal;73             ans += maxVal*e[i].w;74         }75     }76     return ans;77 }78 int main() {79     int u,v,w;80     while(~scanf("%d %d",&n,&m)){81         S = tot = 0;82         T = n+1;83         memset(head,-1,sizeof(head));84         add(S,1,0,2);85         add(n,T,0,2);86         for(int i = 0; i < m; i++){87             scanf("%d %d %d",&u,&v,&w);88             add(u,v,w,1);89             add(v,u,w,1);90         }91         printf("%d\n",solve());92     }93     return 0;94 }
View code

 

Poj 2135 farm tour

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.