网络流(最小费用最大流):POJ 2135 Farm Tour

Farm Tour

Time Limit: 1000ms
Memory Limit: 65536KB
This 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.

Output

A single line containing the length of the shortest tour.

Sample Input

4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output

6

  这题就是在一个无向图中找出两条从点1到点n的路径,同时要求路程最短。
  于是贴最小费用最大流模板就AC啦。
复制代码
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 using namespace std;
 6 const int INF=233333333;
 7 const int maxn=1010,maxm=40010;
 8 int cnt,fir[maxn],nxt[maxm],to[maxm],cap[maxm],val[maxm],dis[maxn],path[maxn];
 9 
10 void addedge(int a,int b,int c,int v)
11 {
12     nxt[++cnt]=fir[a];to[cnt]=b;cap[cnt]=c;val[cnt]=v;fir[a]=cnt;
13 }
14 int S,T;
15 int Spfa()
16 {
17     queue<int>q;
18     memset(dis,127,sizeof(dis));
19     q.push(S);dis[S]=0;
20     while(!q.empty())
21     {
22         int node=q.front();q.pop();
23         for(int i=fir[node];i;i=nxt[i])
24             if(cap[i]&&dis[node]+val[i]<dis[to[i]]){
25                 dis[to[i]]=val[i]+dis[node];
26                 path[to[i]]=i;
27                 q.push(to[i]);
28             }
29     }
30     return dis[T]==dis[T+1]?0:dis[T]; 
31 }
32 
33 int Aug()
34 {
35     int p=T,f=INF;
36     while(p!=S)
37     {
38         f=min(f,cap[path[p]]);
39         p=to[path[p]^1];
40     }
41     p=T;
42     while(p!=S)
43     {
44         cap[path[p]]-=f;
45         cap[path[p]^1]+=f;
46         p=to[path[p]^1];
47     }
48     return f;
49 }
50 
51 int MCMF()
52 {
53     int ret=0,d;
54     while(d=Spfa())
55         ret+=Aug()*d;
56     return ret;    
57 }
58 
59 void Init(int n)
60 {
61     cnt=1;S=0;T=n+1;
62     for(int i=1;i<=n;i++)fir[i]=0;
63 }
64 
65 int main()
66 {
67     int n,m;
68     while(~scanf("%d%d",&n,&m))
69     {
70         Init(n);
71         int a,b,v;
72         for(int i=1;i<=m;i++)
73         {
74             scanf("%d%d%d",&a,&b,&v);
75             addedge(a,b,1,v);
76             addedge(b,a,0,-v);
77             addedge(b,a,1,v);
78             addedge(a,b,0,-v);
79         }
80         addedge(S,1,2,0);
81         addedge(1,S,0,0);
82         addedge(n,T,2,0);
83         addedge(T,n,0,0);
84         printf("%d\n",MCMF());
85     }
86     return 0;
87 }
复制代码

 


posted @   TenderRun  阅读(361)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 2025成都.NET开发者Connect圆满结束
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络
点击右上角即可分享
微信分享提示