题意:给定一个无向图,从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. PTA題目的處理(一)

    **題目1:A乘B** **實驗代碼** #include <stdio.h> #include <stdlib.h> int main() { signed int a,b; ...

  2. NetFPGA-1G-CML从零开始环境配置

    NetFPGA-1G-CML从零开始环境配置 前言 偶得一块NetFPGA-1G-CML,跟着github对NetFPGA-1G-CML的入门指南,一步步把配置环境终于搭建起来,下面重新复现一下此过程 ...

  3. Flask 蓝图(Blueprint)

    蓝图使用起来就像应用当中的子应用一样,可以有自己的模板,静态目录,有自己的视图函数和URL规则,蓝图之间互相不影响.但是它们又属于应用中,可以共享应用的配置.对于大型应用来说,我们可以通过添加蓝图来扩 ...

  4. GPUImage实战问题解决

    在项目中遇到了使用完GPUImage以后,内存不释放的问题,翻阅官方API,找到了解决方法: deinit{ GPUImageContext.sharedImageProcessingContext( ...

  5. 使用Google 的 gson方式解析json

    gson支持解析的类型还是比较全面的,包括JavaBean,List<JavaBean>,List<String>,Map等,使用起来也是比较方便,下面根据代码示例给出总结: ...

  6. 自制 h5 音乐播放器 可搜索

    闲言碎语: 有好几天没有发表博客了,这也是因为一直开发音乐和完善我的博客项目,好不容易抽出时间总结一下这几天所做的东西,笔试又不断通知,实则匆忙 今天难得逃了一次课,就趁这时间,该写写就写写吧~~ 进 ...

  7. LeetCode & Q119-Pascal's Triangle II-Easy

    Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...

  8. NoSQL&MongoDB

    MongoDB: Is NoSQL(技术的实现,并非是一个特定的技术,与RMDS对立):Not only SQL 大数据问题:BigData,eg:同时访问几个页面,代码实现几个页面访问量的大小? F ...

  9. hadoop2.6.0实践:控制台入口url列表

    hadoop web控制台页面的端口整理: 50070:hdfs文件管理 8088:ResourceManager 8042:NodeManager 19888:JobHistory(使用" ...

  10. keepalive配置支持ipv6、ipv4双棧支持

    因公司业务需要,keepalived需要同时支持ipv6和ipv4 keepalived版本1.2.23. keepalived 配置: 重点:ipv6的虚IP配置在 virtual_ipaddres ...