POJ 2135 Farm Tour(最小费用最大流,变形)
题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!)
思路:
有重边,要用邻接表。所给的每条边都要变成4条有向边!否则可能一开始就到达不了终点了。最后要再加上一个源点和汇点,容量cap(源点,1)=2,指定只能走两次,再规定其他所给的边的容量是1就行了,当边被走过了,就自动增加了流,也就走不了了。
解释看代码更清晰。
//#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
//#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+;
vector<int> vect[N];
struct node
{
int from;
int to;
int cost;
int cap;
int flow;
}edge[N*]; //边数是4*M的大小 int edge_num; //边的上限
int f[N]; //流
int path[N]; //记录路径
int c[N]; //记录费用
bool in[N]; //是否在queue中 void add_node(int a, int b, int c, int ca, int fl)
{
edge[edge_num].from=a;
edge[edge_num].to=b;
edge[edge_num].cost=c;
edge[edge_num].cap=ca;
edge[edge_num].flow=fl;
vect[a].push_back(edge_num++);
} LL spfa(int s,int e)
{
deque<int> que;
que.push_back(s);
c[s]=;
f[s]=INF;
in[s]=;
while(!que.empty())
{
int x=que.front();
que.pop_front();
in[x]=;
for(int i=; i<vect[x].size(); i++)
{
node t=edge[vect[x][i]];
if(t.cap>t.flow && c[t.to]>c[t.from]+t.cost) //能流过,且费用更小即可更新。
{
path[t.to]=vect[x][i]; //记边号,方便更新flow
c[t.to]=c[t.from]+t.cost; //更新cost,相当于距离
f[t.to]=min(f[t.from], t.cap-t.flow);
if(!in[t.to])
{
que.push_back(t.to);
in[t.to]=;
}
}
}
}
return c[e];
} int m; int mcmf(int s, int e)
{
LL ans=;
while()
{
memset(f, , sizeof(f));
memset(c, 0x7f, sizeof(c));
memset(in, , sizeof(in));
memset(path, , sizeof(path)); int tmp=spfa(s, e);
if(tmp==INF) return ans;
ans+=tmp*f[e]; //这是最小费用。注:每次的流可能不是1。 int ed=e;
while(ed!=s)
{
int en=path[ed];
edge[en].flow+=f[e];
edge[en^].flow-=f[e];
ed=edge[en].from;
//cout<<edge[en].from<<"-"<<edge[en].to<<endl;
}
}
} int main()
{
freopen("input.txt", "r", stdin);
int n, a, b, c;
while(~scanf("%d%d", &n, &m))
{
for(int i=n*; i>=; i--) vect[i].clear();
edge_num=;
for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
add_node(a,b,c,,); //4条有向边。
add_node(b,a,-c,,); add_node(b,a,c,,);
add_node(a,b,-c,,);
}
add_node(,,,,); //加多2条边
add_node(,,,,); //加这条无意义,但是更新flow时更方便
add_node(n,n+,,,);
add_node(n+,n,,,); printf("%lld\n",mcmf(, n+));
} return ;
}
AC代码
POJ 2135 Farm Tour(最小费用最大流,变形)的更多相关文章
- poj 2135 Farm Tour 最小费用最大流建图跑最短路
题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- [poj] 1235 Farm Tour || 最小费用最大流
原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...
- POJ2135 Farm Tour —— 最小费用最大流
题目链接:http://poj.org/problem?id=2135 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- TZOJ 1513 Farm Tour(最小费用最大流)
描述 When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 &l ...
- Farm Tour(最小费用最大流模板)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18150 Accepted: 7023 Descri ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- poj 2135 Farm Tour 最小费最大流
inf开太小错了好久--下次还是要用0x7fffffff #include<stdio.h> #include<string.h> #include<vector> ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
随机推荐
- 域名转化到IP地址的实现
在linux中,有一些函数可以实现主机名和地址的转化,最常见的有gethostbyname().gethostbyaddr()等,它们都可以实现IPv4和IPv6的地址和主机名之间的转化.其中geth ...
- cadence 焊盘制作小结
因为以前一直用altium designer 话PCB,做封装的时候焊盘是不用自己操心的,但是开始用cadence以后发现好多以前不太懂的东西,需要自己画焊盘,这就导致需要了解好多自己以前不懂的东西, ...
- ENVI栅格文件增强后将LUT保存完输出img图像进行分类
ENVI栅格文件储存 图像原始的DN(Digital Number)值记录图像的光谱信息,不能轻易更改。在窗口中显示的一般是经过拉伸等增强处理的LUT上的灰度值,在保存文件时,就有不同的方式。 1. ...
- AirDrop显示名字的修改问题
AirDrop的名字来源是设备登陆的iCloud账户 打开iCloud设置 把个人信息的名字改成自己的即可 前提是你的账号没有借朋友用过,如果朋友用过恰好没注销,你的通讯录又有你的朋友的号码,很有可能 ...
- A geometric interpretation of the covariance matrix
A geometric interpretation of the covariance matrix Contents [hide] 1 Introduction 2 Eigendecomposit ...
- Exploring the 7 Different Types of Data Stories
Exploring the 7 Different Types of Data Stories What makes a story truly data-driven? For one, the n ...
- 【技术贴】webservice cxf2 客户端动态调用报错No operation was found with the name
No operation was found with the name xxx 出错原因是因为发布服务的接口所在包路径和此接口实现类包路径不一致,比如你的服务接口可能放在了包com.x.interF ...
- Android支付接入(四):联通VAC计费
原地址:http://blog.csdn.net/simdanfeg/article/details/9012031 注意事项: 1.联通支付是不需要自己标识软硬计费点的,当平台申请计费点的时候会提交 ...
- 漫话C++0x(五)—- thread, mutex, condition_variable
熟悉C++98的朋友,应该都知道,在C++98中没有thread, mutex, condition_variable这些与concurrency相关的特性支持,如果需要写多线程相关程序,都要借助于不 ...
- http://doc.okbase.net/congcong68/archive/112508.html
http://doc.okbase.net/congcong68/archive/112508.html