题意:从1到n再到1,每条边只能走一次,求最短距离。

建图:每条边只能走一次就是流量是1,添加源点与1相连,容量为2,费用为0,n与汇点相连容量为2,费用为0;

求增广路用SPFA最短路求,,






#include<stdio.h>
#include<queue>
#include<string.h>
const int N=1100;
const int inf=0x3fffffff;
using namespace std;
int cost[N],start,end,n,head[N],num,pre[N],vis[N];
struct edge
{
int st,ed,cp,flow,next;
}e[N*N];
void addedge(int x,int y,int c,int w)
{
e[num].st=x;e[num].ed=y;e[num].cp=c; e[num].flow=w;e[num].next=head[x];head[x]=num++;
e[num].st=y;e[num].ed=x;e[num].cp=-c;e[num].flow=0;e[num].next=head[y];head[y]=num++;
}
int SPFA()
{
int i,u,v;
queue<int>Q;
for(i=start;i<=end;i++)
{cost[i]=inf;vis[i]=0;pre[i]=-1;}
cost[start]=0;vis[start]=1;
Q.push(start);
while(!Q.empty())
{
u=Q.front();
Q.pop();vis[u]=0;
for(i=head[u];i!=-1;i=e[i].next)
{
v=e[i].ed;
if(e[i].flow>0&&cost[v]>cost[u]+e[i].cp)
{
pre[v]=i;
cost[v]=cost[u]+e[i].cp;
if(vis[v]==0)
{
vis[v]=1;
Q.push(v);
}
}
}
}
if(pre[end]==-1)
return 0;
return 1;
}
int mincost()
{
int MINcost=0,maxflow=0,i,minflow;
while(SPFA())
{
minflow=inf;
for(i=pre[end];i!=-1;i=pre[e[i].st])
if(minflow>e[i].flow)
minflow=e[i].flow;
maxflow+=minflow;
for(i=pre[end];i!=-1;i=pre[e[i].st])
{
e[i].flow-=minflow;
e[i^1].flow+=minflow;
MINcost+=e[i].cp;
}
}
return MINcost;
}
int main()
{
int i,x,y,c,m;
while(scanf("%d%d",&n,&m)!=-1)
{
start=0;end=n+1;num=0;
memset(head,-1,sizeof(head));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&c);
addedge(x,y,c,1);
addedge(y,x,c,1);
}
addedge(start,1,0,2);
addedge(n,end,0,2);
printf("%d\n",mincost());
}
return 0;
}

poj 2135 (基础费用流)的更多相关文章

  1. POJ 2516 基础费用流

    题意       有n个顾客,m个供应商,k种货物,给你顾客对于每种货物的要求个数,和供应商对于每种货物的现有量,以及供应每种货物的时候供应商和顾客之间的运输单价,问你满足所有顾客的前提下的最小运输费 ...

  2. POJ 2135 简单费用流

    题意:       题意是一个人他要从牧场1走到牧场n然后在走回来,每条路径只走一次,问全程的最短路径是多少. 思路:        这个题目挺简单的吧,首先要保证每条边只能走一次,然后还要要求费用最 ...

  3. POJ 2135 最小费用最大流

    题目链接 Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18961   Accepted: 7326 D ...

  4. POJ 2135 最小费用最大流 入门题

    Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19207   Accepted: 7441 Descri ...

  5. poj 2135最小费用最大流

    最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...

  6. poj 3680 Intervals(费用流)

    http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第i+1个点 ...

  7. POJ 2175 spfa费用流消圈

    题意:给出n栋房子位置和每栋房子里面的人数,m个避难所位置和每个避难所可容纳人数.然后给出一个方案,判断该方案是否最优,如果不是求出一个更优的方案. 思路:很容易想到用最小费用流求出最优时间,在与原方 ...

  8. POJ 1273 (基础最大流) Drainage Ditches

    虽然算法还没有理解透,但以及迫不及待地想要A道题了. 非常裸的最大流,试试lrj的模板练练手. #include <cstdio> #include <cstring> #in ...

  9. POJ 2135 /// 最小费用流最大流 非负花费 BellmanFord模板

    题目大意: 给定一个n个点m条边的无向图 求从点1去点n再从点n回点1的不重叠(同一条边不能走两次)的最短路 挑战P239 求去和回的两条最短路很难保证不重叠 直接当做是由1去n的两条不重叠的最短路 ...

随机推荐

  1. Uva 5002 - The Queue DFS

    On some special occasions Nadia’s company provide very special lunch for all employees of the compan ...

  2. vue过滤器在v2.0版本用法

    vue 1.x 的写法在  vue 2.x版本已经废除 vue 1.x 写法 <body> <div id="app"> {{message | capit ...

  3. GIT(1)----更新代码和上传代码操作的步骤

    1.第一次下载代码 a.首先获得下载的地址,可从服务器,或者GitHut上获得.例如http://100.211.1.110:21/test/test.git b.终端里切换到想要将代码存放的目录,在 ...

  4. oracle_创建表空间、用户、授权等步骤

    --创建表空间(在system用户下才可以创建) //connect system/manager as sysdba:切换到system用户 create tablespace tz datafil ...

  5. Syslink Control in MFC 9.0(转)

    Visual Studio 2008 (formely code-named ‘Orcas’) has several important updates for VC++ and MFC. Amon ...

  6. Namespace declaration statement has to be the very first statement in the script

    php 中 Namespace declaration statement has to be the very first statement in the script 错误解决方法: 在PHP文 ...

  7. 安装oracle 11gr2 rac on solaris

    http://candon123.blog.51cto.com/704299/389470

  8. systemtap 2.8 安装说明书

    systemtap: a linux trace/probe tool Visit the project web site at <http://sourceware.org/systemta ...

  9. Leptonica在VS2010中的编译及简单使用举例

    在tesseract-ocr中会用到leptonica库.这里对leptonica简介下. Leptonica是一个开源的图像处理和图像分析库,它的license是BSD 2-clause.它主要包括 ...

  10. 演示Microsoft Advertising SDK for Windows Phone 8.1

    演示Microsoft Advertising SDK for Windows Phone 8.1,Only for Windows Phone 8.1 1.在References上点右键,添加引用, ...