poj 2135 Farm Tour 最小费最大流
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 最小费最大流的更多相关文章
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
- poj 2135 Farm Tour 最小费用最大流建图跑最短路
题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- POJ 2135 Farm Tour(最小费用最大流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
随机推荐
- 【Java_基础】Java中Native关键字的作用
本篇博文转载与:Java中Native关键字的作用
- 玩转ApplicationContextAware
当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean.换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象.结合工厂 ...
- js 字符串加密
加密: 1.获得要加密的字符串:var str=input.value; 2.转化: for(var i=0;i<str.length;i++){ str+=String.fromCharCod ...
- 【STL初步】不定长数组:vector + 集合:set + 映射:map
一.vector 为了节省空间,有时我们会使用动态数组vector. 定义动态数组 vector<类型名>变量名 vector<int>que //定义que为一个int类型的 ...
- 剑指Offer(书):调整数组顺序使奇数位于偶数前面
题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. public void ...
- java读写串口数据
本博文参考自https://www.cnblogs.com/Dreamer-1/p/5523046.html 最近接触到了串口及其读写,在此记录java进行串口读写的过程. 1.导入串口支持包 需要下 ...
- C#保存图片到文件夹区分8位和24位
1.保存图像--24位位图(显示的图像,包括增加结果到界面上的数据) Image image2 = default(Image); image2 = cogRecordDisplay1.CreateC ...
- Linux下文件打包与解包
打包(.tar): tar -cvf Pro.tar /home/lin/Pro #将/home/lin/Pro文件夹下的所有文件打包成Pro.tar 打解包(.tar.gz) tar -cv ...
- Windows同步阿里云时间
Ctrl+R打开cmd命令框 输入:gpedit.msc 计算机配置”—“管理模版”—“系统”—“Windows 时间服务”—“时间提供程序”—“配置 Windows NTP 客户端 双击打开配置 W ...
- MySQL5.7 MTS work线程stack
复制现象是,slave线程状态正常,但是sql 线程不应用,所以delay越来越大,查看复制状态 mysql> show slave status\G********************** ...