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. 获取元素在浏览器中的绝对位置(从jquery1.8中抠出来)

    <style> html,body{margin:0;padding:0;} .d1{margin-left:40px;background:red;width:2000px;height ...

  2. JavaScript---基本语法

    字符串方法:str.lengthstr.charAt(i):取字符串中的某一个;str.indexOf('e');找第一个出现的位置;找不到返回-1;str.lastIndexOf('e'):找最后一 ...

  3. Wordpress 3.5.1的debug流水账

    一个合作网站是基于Wordpress做的, 不能正常使用FeedWordPress这个插件, 对方没有开发者, 只有一个类似于美工和编辑的人, 因为我在本地使用同样的版本(Wordpress 3.5. ...

  4. SpringMVC视图解析器(转)

    前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视图解析器.当我们对SpringMVC控制的资源发起请求时,这些请求都会被Sprin ...

  5. GeoServer 常见问题总结

    Geoserver安装环境 Geoserver在部署发布服务时,经常会遇到如下问题,现总结如下: 1.忘记了GeoServer Web Admin Page的登陆用户名和密码怎么办? 存储位置:C:\ ...

  6. OAF中 遍历HGrid组件中的所有VO行

    在HGrid组件中有如下所示的HeaderVO和LineVO 需要在头上的LOV中触发事件去更新行VO中的值,LOV事件的处理方法见 getLovParameter ,但是由于HGrid的特殊性,不能 ...

  7. 逆向最大匹配分词算法C#

    逆向顺序 句子:大家好我叫XX我是一名程序员 程序员 -> 序员 -> 员 名程序 -> 程序 -> 序 一名程 -> 名程 -> 程 是一名 -> 一名 - ...

  8. python 字符编码练习

    通过下面的练习,加深对python字符编码的认识 # \x00 - \xff 256个字符 >>> a = range(256)>>> b = bytes(a) # ...

  9. python 操作注册表

    import win32api import win32con keyname = r'Software\Microsoft\Internet Explorer\Main' page = 'www.l ...

  10. ubuntu16.04下安装jdk和android studio

    1首先要在JDK官网下载对应的Linux的JDK版本.进入该网站后,先选择Accept License Agreement然后即可下载.本人的Linux系统为ubuntukylin 16.04  64 ...