题目:

id=2135" target="_blank">poj 2135 Farm Tour

题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路。

分析:这个题目不读细致的话可能会当做最短路来做,最短路求出来的不一定是最优的,他是两条分别最短,但不一定是和最短。

我们能够用费用流来非常轻易的解决,建边容量为1,费用为边权。然后源点s连 1 。费用0 。容量 2 ,n点连接汇点,容量2,费用0,,就能够了。

注意这个题目是无向图,所以要建双向边。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int N = 1050;
const int inf = 0x3f3f3f3f;
#define Del(a,b) memset(a,b,sizeof(a))
struct Node
{
int from,to,cap,flow,cost;
};
vector<int> v[N];
vector<Node> e;
void add_Node(int from,int to,int cap,int cost)
{
e.push_back((Node){from,to,cap,0,cost});
e.push_back((Node){to,from,0,0,-cost});
int len = e.size()-1;
v[to].push_back(len);
v[from].push_back(len-1);
}
int vis[N],dis[N];
int father[N],pos[N];
bool BellManford(int s,int t,int& flow,int& cost)
{
Del(dis,inf);
Del(vis,0);
queue<int> q;
q.push(s);
vis[s]=1;
father[s]=-1;
dis[s] = 0;
pos[s] = inf;
while(!q.empty())
{
int f = q.front();
q.pop();
vis[f] = 0;
for(int i=0; i<v[f].size(); i++)
{
Node& tmp = e[v[f][i]];
if(tmp.cap>tmp.flow && dis[tmp.to] > dis[f] + tmp.cost)
{
dis[tmp.to] = dis[f] + tmp.cost;
father[tmp.to] = v[f][i];
pos[tmp.to] = min(pos[f],tmp.cap - tmp.flow);
if(vis[tmp.to] == 0)
{
vis[tmp.to]=1;
q.push(tmp.to);
}
}
}
}
if(dis[t] == inf)
return false;
flow += pos[t];
cost += dis[t]*pos[t];
for(int u = t; u!=s ; u = e[father[u]].from)
{
e[father[u]].flow += pos[t];
e[father[u]^1].flow -= pos[t];
}
return true;
}
int Mincost(int s,int t)
{
int flow = 0, cost = 0;
while(BellManford(s,t,flow,cost)){}
return cost;
}
void Clear(int x)
{
for(int i=0; i<=x; i++)
v[i].clear();
e.clear();
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add_Node(x,y,1,z);
add_Node(y,x,1,z);
}
int s = 0 ,t = n+1;
add_Node(s,1,2,0);
add_Node(n,t,2,0);
int ans = Mincost(s,t);
printf("%d\n",ans);
Clear(n+1);
}
return 0;
}

poj 2135 Farm Tour 【无向图最小费用最大流】的更多相关文章

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

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

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

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

  3. POJ 2135 Farm Tour(最小费用最大流,变形)

    题意:给一个无向图,FJ要从1号点出发到达n号点,再返回到1号点,但是路一旦走过了就会销毁(即回去不能经过),每条路长度不同,那么完成这趟旅行要走多长的路?(注:会有重边,点号无序,无向图!) 思路: ...

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

    第一道费用流的题目--- 其实---还是不是很懂,只知道沿着最短路找增广路 建图 源点到1连一条容量为2(因为要来回),费用为0的边 n到汇点连一条容量为2,费用为0的边 另外的就是题目中输入的了 另 ...

  5. POJ2135 Farm Tour(最小费用最大流)

    题目问的是从1到n再回到1边不重复走的最短路,本质是找1到n的两条路径不重复的尽量短的路. #include<cstdio> #include<cstring> #includ ...

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

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

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

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

  8. POJ 2135.Farm Tour 消负圈法最小费用最大流

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4914   Accepted: 1284   ...

  9. POJ 2195 Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意 :  N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...

随机推荐

  1. JavaScript 判断手机端操作系统(Andorid/IOS)

    androidURL = "http://xxx/xxx.apk"; var browser = { versions: function() { var u = navigato ...

  2. Java接口中的成员变量为什么必须声明为public static final?

    我想对于每个Java程序员来说,接口都不陌生,接口中的方法也经常使用.而接口中的成员变量,就显得用得少一点,而对于成员变量为什么必须声明为public static final,可能就更不清楚了,而且 ...

  3. Android 复制文本内容到系统剪贴板(自由复制)

    直接上代码:(对应的类:android.content.ClipboardManager) //获取剪贴板管理器: ClipboardManager cm = (ClipboardManager) g ...

  4. Python之first script

    1 A first script 1) script1.py - imports a Python module (libraries of additional tools) to fetch th ...

  5. WebGL画点程序v3

    本文程序实现画一个点的任务,如下图.其中,点的颜色由Javascript传到片元着色器程序中. 整个程序包含两个文件,分别是: 1. HelloPoint3.html <!DOCTYPE HTM ...

  6. (转)Bootstrap 之 Metronic 模板的学习之路 - (2)源码分析之 head 部分

    https://segmentfault.com/a/1190000006684122 下面,我们找个目录里面想对较小的文件来分析一下源码结构,我们可以看到,page_general_help.htm ...

  7. tomcat映射java目录 sever.xml

    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />        --> & ...

  8. Mysql [Err] 1118 - Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535.

    对于越来越多的数据,数据库的容量越来越大,压缩也就越来越常见了.在我的实际工作中进行过多次压缩工作,也遇到多次问题,在此和大家分享一下. 首先,我们先说说怎么使用innodb的压缩. 第一,mysql ...

  9. Network-Flow

    //Created by pritry int graph[MAX][MAX]; //原图 int source; //起点,这里为0 int sink; //终点,这里为n-1 int e[MAX] ...

  10. Linux下进程与线程的区别

    https://www.cnblogs.com/fah936861121/articles/8043187.html https://my.oschina.net/cnyinlinux/blog/36 ...