UVALIVE 6905 Two Yachts(最小费用最大流)

#include <bits/stdc++.h>

using namespace std;
const int maxn=11111;
const int inf=0x3f3f3f3f;
int t,n;
int u[maxn],v[maxn],w[maxn],head[maxn<<1];
int x[maxn<<1],cnt,ecnt;
int d[maxn<<1],p[maxn<<1],inq[maxn<<1],a[maxn<<1];
struct Edge{
    int u,v,cost,cap,flow,p;
    Edge(){};
    Edge(int u,int v,int cost,int cap,int flow,int p):u(u),v(v),cost(cost),cap(cap),flow(flow),p(p){};
}e[maxn<<4];
void add(int u,int v,int c,int f){
    e[ecnt]=Edge(u,v,c,f,0,head[u]);
    head[u]=ecnt++;
}
bool spfa(int s,int t){
    memset(d,0x3f,sizeof d);
    memset(inq,0,sizeof inq);
    d[s]=0;inq[s]=1;a[s]=inf;
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int u=q.front();q.pop();
        inq[u]=0;
        for(int i=head[u];~i;i=e[i].p){
            int v=e[i].v;
            if(e[i].cap>e[i].flow&&d[v]>d[u]+e[i].cost){
                d[v]=d[u]+e[i].cost;
                p[v]=i;
                a[v]=min(a[u],e[i].cap-e[i].flow);
                if(!inq[v]){
                    q.push(v);
                    inq[v]=1;
                }
            }
        }
    }
    return d[t]!=inf;
}
int mcmf(int s,int t){
    int ans=0;
    while(spfa(s,t)){
        ans+=d[t];
        int u=t;
        while(u!=s){
            e[p[u]].flow+=a[t];
            e[p[u]^1].flow-=a[t];
            u=e[p[u]].u;
        }
    }
    return ans;
}
int main()
{
//    freopen("in","r",stdin);
    cin>>t;
    while(t--){
        scanf("%d",&n);
        memset(head,-1,sizeof head);
        cnt=0;
        for(int i=0;i<n;i++){
            scanf("%d%d%d",u+i,v+i,w+i);
            x[cnt++]=u[i];
            x[cnt++]=++v[i];
        }
        sort(x,x+cnt);
        cnt=unique(x,x+cnt)-x;
        for(int i=0;i<n;i++){
            u[i]=lower_bound(x,x+cnt,u[i])-x+1;
            v[i]=lower_bound(x,x+cnt,v[i])-x+1;
        }
        int s=0,t=cnt+1;
        for(int i=0;i<t;i++){
            add(i,i+1,0,2);
            add(i+1,i,0,0);
        }
        for(int i=0;i<n;i++){
            add(u[i],v[i],-w[i],1);
            add(v[i],u[i],-w[i],-1);
        }
        printf("%d\n",-mcmf(s,t));
    }
    return 0;
}
时间: 2024-08-16 06:05:28

UVALIVE 6905 Two Yachts(最小费用最大流)的相关文章

【BZOJ3876】【Ahoi2014】支线剧情 有下界的最小费用最大流

#include <stdio.h> int main() { puts("转载请注明出处谢谢"); puts("http://blog.csdn.net/vmurder/article/details/43025375"); } [BZOJ2324]营救皮卡丘 这道题也是一道有下界的最小费用最大流. 我的题解地址:http://blog.csdn.net/vmurder/article/details/41378979 这道题其实就是模板题. 我的处理

POJ 3686.The Windy&#39;s 最小费用最大流

The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5477   Accepted: 2285 Description The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The ma

P3381 【模板】最小费用最大流

P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行包含四个正整数ui.vi.wi.fi,表示第i条有向边从ui出发,到达vi,边权为wi(即该边最大流量为wi),单位流量的费用为fi. 输出格式: 一行,包含两个整数,依次为最大流量和在最大流量情况下的

C++之路进阶——最小费用最大流(支线剧情)

F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  hyxzc Logout 捐赠本站 Notice:由于本OJ建立在Linux平台下,而许多题的数据在Windows下制作,请注意输入.输出语句及数据类型及范围,避免无谓的RE出现. 3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 542  Solved: 332[Submit

hdu 4494 Teamwork 最小费用最大流

Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 Description Some locations in city A has been destroyed in the fierce battle. So the government decides to send some workers to repair these location

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

1.N*M的矩阵中,有k个人和k个房子,每个人分别进入一个房子中,求所有人移动的最小距离. 2.人看成源点,房子看成汇点,求最小费用最大流. 建图-- 人指向房子,容量为1,费用为人到房子的曼哈顿距离. 建立超级源点和超级汇点:超级源点指向人,容量为1,费用为0:超级汇点指向房子,容量为1,费用为0. 求超级源点到超级汇点的最小费用最大流即可. ps:容量为什么都设为1?---有待研究.. 3. 1.Bellman-Ford: #include<iostream> #include<st

hdu 1853 Cyclic Tour 最小费用最大流

题意:一个有向图,现在问将图中的每一个点都划分到一个环中的最少代价(边权和). 思路:拆点,建二分图,跑最小费用最大流即可.若最大流为n,则说明是最大匹配为n,所有点都参与,每个点的入度和出度又是1,所以就是环. /********************************************************* file name: hdu1853.cpp author : kereo create time: 2015年02月16日 星期一 17时38分51秒 *******

最小费用最大流

Farm Tour http://poj.org/problem?id=2135 建图再说吧 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cmath> 5 #include<map> 6 #include<stack> 7 #include<queue> 8 #include<vector> 9 #include&l

POJ 2195 地图的最小费用最大流

思路:这题刚开始看就知道是最小费用最大流了,因为求出最优嘛,而且要m,H要一一对应,所以不是二分图匹配就是最小费用最大流. 不过,刚开始还在想每个m与H之间的最小花费如何求,难道要用dfs搜索吗?这样想之后看了下题目给的时间是1000ms,然后就把dfs搜索m与H之间的最短距离排除了.然后想了想,其实尼玛太简单了,因为题目说了只能垂直与竖直的走,所以最短距离不就是两个横坐标相减与两个纵坐标相减之和嘛! 然后每对m与H之间都连边,流量为1(因为每对匹配不能重复),费用为它们之间的距离即花费:然后建

BZOJ 2324 营救皮卡丘(最小费用最大流)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2324 题意:n+1个城市(0到n).初始时K个 人都在0城市.城市之间有距离.要求(1)遍历完n个城市(有一个人遍历了某个城市就算这个城市被遍历了):(2)遍历i城市前必须遍历完前i-1个城 市,并且在遍历前i-1个城市时不能经过大于等于i的城市.在满足(1)(2)的前提下使得K个人走的总距离最小. 思路:我们先看在实际情况下可以怎么走. (1)某个人遍历完某个城市后停在那里,以后不再