inf开太小错了好久……下次还是要用0x7fffffff

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int N=5024;
const int inf=0x7fffffff;
struct Edge
{
int from,to,cap,flow,cost;
};
vector<Edge>edges;
vector<int>G[N];
int n,m;
int inq[N],p[N],d[N],a[N]; void AddEdge(int from, int to,int cap, int cost)
{
Edge tp;
tp.from=from,tp.to=to,tp.cap=cap,tp.flow=0,tp.cost=cost;
edges.push_back(tp); tp.from=to,tp.to=from,tp.cap=0,tp.flow=0,tp.cost=-cost;
edges.push_back(tp); int g=edges.size();
G[from].push_back(g-2);
G[to].push_back(g-1);
} int BellmanFord(int s,int t,int &flow, int &cost)
{
int i,j,u;
for(i=0; i<=n+1; i++) d[i]=inf;
memset(inq,0,sizeof(inq));
d[s]=0;
inq[s]=1;
p[s]=0;
a[s]=inf;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
u=Q.front();
Q.pop();
inq[u]=0;
for(i=0; i<G[u].size(); i++)
{
Edge &e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
{
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(d[t]==inf) return 0;
flow+=a[t];
cost+=d[t]*a[t];
u=t;
while(u!=0)
{
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
u=edges[p[u]].from;
}
return 1;
} int Mincost(int s,int t)
{
int flow=0,cost=0;
while(BellmanFord(s,t,flow,cost));
return cost;
} int main()
{
int i,u,v,c;
while(~scanf("%d%d",&n,&m))
{
for(i=0; i<=n+1; i++) G[i].clear();
edges.clear();
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&c);
AddEdge(u,v,1,c);
AddEdge(v,u,1,c);
}
AddEdge(0,1,2,0);
AddEdge(n,n+1,2,0);
printf("%d\n",Mincost(0,n+1));
}
return 0;
}

poj 2135 Farm Tour 最小费最大流的更多相关文章

  1. POJ 2135 Farm Tour [最小费用最大流]

    题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...

  2. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

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

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

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

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

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

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

  6. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

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

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

  8. POJ 2135 Farm Tour(最小费用最大流)

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

  9. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

随机推荐

  1. python基础知识13-迭代器与生成器,导入模块

    异常处理作业讲解 file = open('/home/pyvip/aaa.txt','w+') try: my_dict = {'name':'adb'} file.write(my_dict['a ...

  2. shell-code-6-输入输出重定向

    解释: 1. 文件描述符0通常是标准输入(STDIN,终端),1 是标准输出(STDOUT,终端),2 是标准错误输出(STDERR). 2. 如果希望 stderr 追加到 file 文件末尾,可以 ...

  3. STM32L0开发——ADC多通道采集,IDE和IAR开发注意事项

    keil开发L0系列是免费的,官方提供许可的.因此建议Keil开发,L011F3由于flash只有8K,因此不建议HAL库,建议使用cubemx+LL(或snippets库).0.起初,可以参考官方库 ...

  4. 基础训练 FJ的字符串

    FJ的字符串 #include<iostream> #include<string.h> using namespace std; int main(){ string str ...

  5. go 和make的用法 区别

    Doand Make are two verbs which frequently confuse students of English. Learn the Difference between ...

  6. IOS 自动布局-UIStackPanel和UIGridPanel(二)

    在上一篇中我提到了如何使用stackpanel和gridpanel来实现自动布局.而在这一篇中我着重讲解下其中的原理. 在(UIPanel   UIStackPanel  UIGridPanel)中主 ...

  7. python--getitem一拦截索引运算

    getitem一拦截索引运算 __getitem__方法拦截实例的索引运算.当实例x出现在x[i]这样的索引运算中时,Python会调用这个实例继承的__getitem__方法(如果有的话),把x作为 ...

  8. LCA+主席树 (求树上路径点权第k大)

      SPOJ 10628. Count on a tree (树上第k大,LCA+主席树) 10628. Count on a tree Problem code: COT You are given ...

  9. BZOJ 1294 [SCOI2009]围豆豆Bean ——计算几何

    显然我们不可能表示出一台路径,因为实在是太复杂了. 所以我们可以记录一下路径对答案的影响,显然路径对答案影响相同的时候,答案更优,所以我们可以用影响来代替路径. 所以我们考虑状压一下所有的豆子有没有被 ...

  10. spring之注入类型

    spring有三种注入类型: set注入: 构造注入: 接口注入: 一.set注入(引用spring官方文档中的例子)(用的最多) 1.首先在代码中我们需要编写成员变量的set方法,如下所示,一般情况 ...