题意:给定一个无向图,从1走到n再从n走回1,每个边只能走一遍,求最短路

题解:可以定义一个源点s,和一个汇点t

s和1相连容量为2,费用为0,

t和n相连容量为2,费用为0

然后所用的边的容量都定为1,跑一遍最小费用最大流即可

 #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#define MAXN 1000+10
#define INF 0x7f7f7f7f
#define ll long long
using namespace std;
struct Edge{
int from,to,cap,flow,cost;
Edge(int u=,int v=,int c=,int f=,int w=){
from=u,to=v,cap=c,flow=f,cost=w;
}
};
int n,m;
vector<Edge> edges;
vector<int> G[MAXN];
int d[MAXN];
int b[MAXN];
int a[MAXN];
int p[MAXN];
void AddEdge(int u,int v,int cap,int cost){
edges.push_back(Edge(u,v,cap,,cost));
edges.push_back(Edge(v,u,,,-cost));
int t=edges.size();
G[u].push_back(t-);
G[v].push_back(t-);
}
int SPFA(int s,int t,int &flow,ll &cost){
memset(d,0x7f,sizeof(d));
memset(b,,sizeof(b)); queue<int> q;
q.push(s);
b[s]=;
d[s]=;
a[s]=INF;
p[s]=; while(!q.empty()){
int x=q.front(); q.pop();
b[x]=;
for(int i=;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(e.cap>e.flow&&d[e.to]>d[x]+e.cost){
d[e.to]=d[x]+e.cost;
a[e.to]=min(a[x],e.cap-e.flow);
p[e.to]=G[x][i];
if(!b[e.to]){
b[e.to]=;
q.push(e.to);
}
}
}
}
if(d[t]==INF){
return ;
} flow+=a[t];
cost+=1LL*a[t]*d[t];
for(int x=t;x!=s;x=edges[p[x]].from){
edges[p[x]].flow+=a[t];
edges[p[x]^].flow-=a[t];
}
return ;
}
ll MincostMaxflow(int s,int t){
int flow=;
ll cost=;
while(SPFA(s,t,flow,cost));
return cost;
}
void solve(){
printf("%lld\n",MincostMaxflow(,n+));
}
void init(){
memset(a,,sizeof(a));
memset(p,,sizeof(p));
edges.clear();
for(int i=;i<=n;i++){
G[i].clear();
}
for(int i=;i<=m;i++){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
u++,v++;
AddEdge(u,v,,w);
AddEdge(v,u,,w);
}
AddEdge(,,,);
AddEdge(n+,n+,,);
}
int main()
{
while(~scanf("%d%d",&n,&m)){
init();
solve();
}
return ;
}

POJ2135:Farm Tour的更多相关文章

  1. POJ2135:Farm Tour——题解

    http://poj.org/problem?id=2135 题目大意: 从1到n再回来,每条边只能走一次,问最短路. —————————————————— 如果不告诉我是费用流打死不会想这个…… 我 ...

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

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

  3. POJ2135 Farm Tour

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

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

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

  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 Farm Tour

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

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

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

  8. poj 2351 Farm Tour (最小费用最大流)

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17230   Accepted: 6647 Descri ...

  9. poj 2135 Farm Tour 【无向图最小费用最大流】

    题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...

随机推荐

  1. Flask 扩展 自定义扩展

    创建一个为视图访问加日志的扩展Flask-Logging,并从中了解到写Flask扩展的规范. 创建工程 先创建一个工程,目录结构如下: flask-logging/ ├ LICENSE # 授权说明 ...

  2. Struts2之配置

    Struts2的默认配置文件是struts.xml放在/web-inf/classes目录下,struts配置文件的最大作用就是配置Action与请求之间的对应关系,并配置逻辑视图名和物理视图名之间的 ...

  3. bzoj千题计划251:bzoj3672: [Noi2014]购票

    http://www.lydsy.com/JudgeOnline/problem.php?id=3672 法一:线段树维护可持久化单调队列维护凸包 斜率优化DP 设dp[i] 表示i号点到根节点的最少 ...

  4. 20170222==(MODBUS读取多个寄存器)

    MODBUS读取多个寄存器(功能码04) 为了简单我这里只用4个寄存器,当让你也可以用125个寄存器,但是最多也只能用125个寄存器的.每个寄存器有上面的表知道为一个字的大小即2个字节或者叫16比特位 ...

  5. 关于kali linux 2.0的vmware tools的安装问题

    在安装好kali linux 2.0 后,首先要做的就是添加源并更新系统,否则会出现软件定位问题. 在kali 2.0中,vmware tools已经不能使用了,官方放了一个工具下载安装就好. 添加源 ...

  6. 算法题丨Two Sum

    描述 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  7. node express将请求重定向为https

    项目开发时,由于服务器只接受https请求(运维说了算...),所以在生产环境时,要把所有http请求全都重定向为https,具体操作是在app.js文件里加入以下代码: var express = ...

  8. linux——网络基础

    装完linux系统要对网络(ip地址,子网掩码,网关,DNS)进行配置,才能连接网络 一,开启网卡eth0 CentOS显示没有网卡(eth0) 2.设置静态IP vim /etc/sysconfig ...

  9. 算法题丨4Sum

    描述 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  10. 阿里云API网关(4)快速入门(开放 API)

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...