费用流!

来源:互联网 发布:python 微秒延时 编辑:程序博客网 时间:2024/04/24 04:31

Description

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000. 

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again. 

He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.

Input

* Line 1: Two space-separated integers: N and M. 

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length. 

Output

A single line containing the length of the shortest tour. 

Sample Input

4 51 2 12 3 13 4 11 3 22 4 2

Sample Output

6

基本思路:
    把弧<i,j>的单位费用w[i,j]看作弧<i,j>的路径长度,每次找从源点s到汇点t长度最短(费用最小)的可增广路径进行增广。
1. 最小费用可增广路
2. 路径s到t的长度即单位流量的费用。
其实就是把ek算法中的广搜改成spfa……(因为存在负权边所以得用bellman-ford或spfa)
<pre name="code" class="cpp">#include"stdio.h"#include"string.h"#include"queue"using namespace std;#define M 1100const int inf=0x7fffffff;struct node{    int u,v,c,f,next;  //C为花费,F为flow流量}e[M*40];int pre[M],dis[M],head[M],t;int vis[M];void add1(int u,int v,int c,int f){    e[t].u=u;    e[t].v=v;    e[t].c=c;    e[t].f=f;    e[t].next=head[u];    head[u]=t++;}void add(int u,int v,int c,int f){    add1(u,v,c,f);          add1(v,u,-c,0);  //反向边流量初始为零,如果走反向边费用正好和原边抵消}int spfa(int s,int t){    int i,u,v;    queue<int>q;    q.push(s);    memset(vis,0,sizeof(vis));    memset(pre,-1,sizeof(pre));    for(i=s;i<=t;i++)        dis[i]=inf;    dis[s]=0;    while(!q.empty())    {        u=q.front();        q.pop();        for(i=head[u];i!=-1;i=e[i].next)        {            v=e[i].v;            if(e[i].f&&dis[v]>dis[u]+e[i].c)  //找到一条最小费用流            {                dis[v]=dis[u]+e[i].c;                  pre[v]=i;       //记录路径                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }        vis[u]=0;    }    if(dis[t]!=inf)        return 1;    return 0;}void solve(int s,int t){    int ans=0,i,j;int flow=0,cost=0;     //总流量、总费用    while(spfa(s,t))    {int minf=inf;for(i=pre[t];i!=-1;i=pre[e[i].u]){if(e[i].f<minf)minf=e[i].f;}flow+=minf;   //该条路径的流量        for(i=pre[t];i!=-1;i=pre[e[i].u])        {            j=i^1;            e[i].f-=minf;            e[j].f+=minf;        }cost+=dis[t]*minf;   //单位运费和乘以流量得费用       }    printf("%d\n",cost);}int main(){    int i,u,v,c,n,m;    while(scanf("%d%d",&n,&m)!=-1)    {        t=0;        memset(head,-1,sizeof(head));        for(i=0;i<m;i++)        {            scanf("%d%d%d",&u,&v,&c);  //边长度看做单位运费            add(u,v,c,1);            add(v,u,c,1);       //无向边,费用为长度,流量为1(只能通过一次)        }//往返的两条线路可以看成是从源点到汇点的两条线路,//所以只要从附加源点向源点连一条容量为2的边和从汇点向附加汇点连一条容量为2的边就可以限线路为两条.        add(0,1,0,2);        add(n,n+1,0,2);        solve(0,n+1);    }    return 0;}




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 b超憋不到尿怎么办 做b超前没有尿意怎么办 肝胆b超前喝水了怎么办 肝胆彩超喝水了怎么办 胆囊b超喝了水怎么办 系统b超照不到怎么办 思维彩超宝宝不动怎么办 怀孕七个月胎儿缺氧怎么办 怀孕29周小孩偏小怎么办 孕29周胎儿臀位怎么办 刚怀孕有囊肿该怎么办 食杏中毒怎么办吃什么 猫吃了扁桃仁怎么办 夏天来了??点狐臭怎么办? 我有一小点狐臭怎么办 淘客网站被微信屏蔽怎么办 微信老是屏蔽网站怎么办 华帝售后没人理怎么办 新开的网店没生意怎么办 淘小二介入买家举证不全怎么办 新股申购中签后钱不够怎么办 买的东西收不到怎么办? 拼多多按错收货怎么办 团购招生做到一半不如意怎么办 网购付款后卖家没有货怎么办 淘宝退货商家不同意退款怎么办 淘宝确认收货后卖家拒绝退款怎么办 淘宝卖家不同意退款怎么办 淘宝仅退款卖家不处理怎么办 淘宝申请退款卖家不处理怎么办 快递没收到货要退款怎么办 发票给了不给钱怎么办 付款后不给发票怎么办 供货商不给发票怎么办 刚生过孩子太胖买衣服怎么办 黑色牛仔裤洗的发白怎么办 蘑菇街手机丢了怎么办 黑衣服上全是白毛毛怎么办 支付宝注销了钱怎么办 网上买东西手机号写错了怎么办 二类工资卡过万怎么办