Time Limit: 2MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Description

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

Source

 
 
POJ的对应页面突然进不去了,没法评测。
目前只确保过了样例
 
最小费用问题
 
 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int INF=;
const int mxn=;
int head[mxn],dis[mxn],pr[mxn];
bool inqu[mxn];
int n,m;
int s,t;
int ans;
int cnt=;
struct edge{
int from,to,next,v,c;
}e[mxn*];
void add_edge(int f,int t,int v,int c){
e[++cnt]=(edge){f,t,head[f],v,c};head[f]=cnt;
e[++cnt]=(edge){t,f,head[t],,-c};head[t]=cnt;
}
bool SPFA(){
queue<int>q;
memset(inqu,false,sizeof(inqu));
for(int i=;i<=t;i++)dis[i]=INF;
dis[s]=;
inqu[s]=;
q.push(s);
int i,j;
while(!q.empty()){
int u=q.front();q.pop();
inqu[u]=false;
for(i=head[u];i;i=e[i].next){
int v=e[i].to;
if(e[i].v && dis[u]+e[i].c<dis[v]){
dis[v]=dis[u]+e[i].c;
pr[v]=i;
if(!inqu[v]){
q.push(v);
inqu[v]=true;
}
}
}
}
return dis[t]!=INF;
}
void mcf(){
int i;
while(SPFA()){
int temp=INF;
for(i=pr[t];i;i=e[pr[i]].from)temp=min(temp,e[i].v);
ans+=dis[t]*temp;
for(i=pr[t];i;i=e[pr[i]].from){
e[i].v-=temp;
e[i^].v+=temp;
}
}
}
int main(){
scanf("%d%d",&n,&m);
s=;t=n+;
int x,y,c;
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&c);
add_edge(x,y,,c);
add_edge(y,x,,c);
}
add_edge(s,,,);
add_edge(n,t,,);
mcf();
printf("%d\n",ans);
}

POJ2135 Farm Tour的更多相关文章

  1. POJ2135 Farm Tour —— 最小费用最大流

    题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  2. poj2135 Farm Tour(费用流)

    Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...

  3. POJ2135 Farm Tour(最小费用最大流)

    题目问的是从1到n再回到1边不重复走的最短路,本质是找1到n的两条路径不重复的尽量短的路. #include<cstdio> #include<cstring> #includ ...

  4. [poj2135]Farm Tour(最小费用流)

    解题关键:最小费用流 代码一:bellma-ford $O(FVE)$  bellman-ford求最短路,并在最短路上增广,速度较慢 #include<cstdio> #include& ...

  5. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

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

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  7. POJ Farm Tour

    Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...

  8. [网络流]Farm Tour(费用流

    Farm Tour 题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm compr ...

  9. Farm Tour(最小费用最大流模板)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18150   Accepted: 7023 Descri ...

随机推荐

  1. AutoIT脚本的语法特征

    这里主要介绍AutoIT的脚本语法特征,包括变量.关键字.宏.设置选项等,详细的语法细节,可以参考其用户手册,也可以去AutoIT中文论坛(www.autoit.net.cn)交流. 1. 变量 Au ...

  2. 使用EditText+ListView并结合TextWatcher实现输入关键字筛选数据

    想必大家应该遇到过这样的情况,当点击Spinner控件后弹出的列表内容超多,一个一个滑动着去寻找所要的项很麻烦,尤其是当自己知道想要选择的内容,这时候如果我们只需要输入某些关键字,就可以讲上百条数据筛 ...

  3. 【转】【C#】在 Windows 窗体 DataGridView 单元格中承载控件

    using System; using System.Windows.Forms; public class CalendarColumn : DataGridViewColumn { public ...

  4. Tasks.Parallel

    .Net多线程编程-System.Threading.Tasks.Parallel   System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Paral ...

  5. 【Andorid------手势识别】GestureDetector和SimpleOnGestureListener的使用教程(转)——

    FROM:http://www.cnblogs.com/transmuse/archive/2010/12/02/1894833.html 1. 当用户触摸屏幕的时候,会产生许多手势,例如down,u ...

  6. C++函数内存占用

    一个类的对象中是没有关于普通成员函数的指针的slot,只有成员变量还有虚表指针,类的成员函数的代码定义在PE文件的代码区,所以从程序加载时,就已经分配好了内存用于存放这些代码:代码运行时所需要的内存, ...

  7. no.5.print sum

    #-*-coding=utf-8-*- for a in range(1,50,1): for b in range(1,50,1): for c in range(1,50,1): if a+b+c ...

  8. WorldWind源码剖析系列:BMNG类构造函数深入分析

    BMNG构造函数深入分析 一.主要类图 二.主要功能: 1)        BMNG类 BMNG类将包含以“Blue Marble”为主题的所有可渲染影像的根节点添加到当前星球的可渲染对象列表中,包括 ...

  9. js屏蔽回车键

    document.onkeydown = function () {            if (window.event && window.event.keyCode == 13 ...

  10. Android 获取手机Mac地址,手机名称

    /** * 获取手机mac地址<br/> * 错误返回12个0 */ public static String getMacAddress(Context context) { // 获取 ...