首先我们可以知道这道题中每个点只能经过一次,那么我们引入附加源汇source,sink,那么我们可以将每个点拆成两个点,分别表示对于图中这个节点我们的进和出,那么我们可以连接(source,i,1,0),(i+n,sink,1,0),然后对于可以直接到达的点我们可以连接(source,i+n,1,cost)这个代表我们可以从任意一个点到达这个点,对于星球之间的连边我们可以连接(x,y+n,1,cost),代表我们可以从这个星球到另一个星球,因为我们考虑每个点只经过一次,所以可以这样构图,我们并不关心路径的具体方案,只关心这个点会被进一次,出一次。

 

/**************************************************************
    Problem: 1927
    User: BLADEVIL
    Language: C++
    Result: Accepted
    Time:2388 ms
    Memory:1540 kb
****************************************************************/
 
//By BLADEVIL
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 2010
#define maxm 40010
 
using namespace std;
 
int n,m,l,source,sink,ans;
int last[maxn],pre[maxm],other[maxm],len[maxm],cost[maxm];
int d[maxn],que[maxn*],vis[maxn],father[maxn];
 
void connect(int a,int b,int c,int d)
{
    pre[++l]=last[a];
    last[a]=l;
    other[l]=b;
    len[l]=c;
    cost[l]=d;
    //if (c) printf("|%d %d %d\n",a,b,d);
}
 
bool spfa()
{
    memset(d,0x3f,sizeof d);
    int h=,t=,cur;
    que[]=source; d[source]=;
    while (h<t)
    {
        cur=que[++h];
        vis[cur]=;
        for (int q=last[cur];q;q=pre[q])
        {
            if (!len[q]) continue;
            if (d[other[q]]>d[cur]+cost[q])
            {
                father[other[q]]=q;
                d[other[q]]=d[cur]+cost[q];
                if (!vis[other[q]])
                {
                    que[++t]=other[q];
                    vis[other[q]]=;
                }
            }
        }
    }
    return d[]!=d[sink];
}
 
void update()
{
    int cur=sink,low=<<;
    while (cur!=source)
    {
        low=min(low,len[father[cur]]);
        cur=other[father[cur]^];
    }
    cur=sink;
    while (cur!=source)
    {
        ans+=cost[father[cur]];
        len[father[cur]]-=low;
        len[father[cur]^]+=low;
        cur=other[father[cur]^];
    }
    //printf("%d\n",ans);
}
 
int main() {
    scanf("%d%d",&n,&m);
    source=(n<<)+; sink=source++; l=;
    for (int i=;i<=n;i++) {
        int x;
        scanf("%d",&x);
        connect(source,i+n,,x); connect(i+n,source,,-x);
    }
    for (int i=;i<=n;i++)   connect(source,i,,),connect(i,source,,);
    for (int i=;i<=m;i++) {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if (x>y) swap(x,y);
        connect(x,y+n,,z); connect(y+n,x,,-z);
    }
    for (int i=;i<=n;i++) connect(i+n,sink,,),connect(sink,i+n,,);
    while (spfa()) update();
    printf("%d\n",ans);
    return ;
}

bzoj 1927 网络流的更多相关文章

  1. Bzoj 1927: [Sdoi2010]星际竞速(网络流)

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Description 10年一度的银河系赛车大赛又要开始了.作为全银河最盛大 ...

  2. bzoj 1927 [Sdoi2010]星际竞速——网络流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1927 每个点拆点保证只经过一次. 主要是如果经过了这个点,这个点应该向汇点流过去表示经过了它 ...

  3. BZOJ 1927: [Sdoi2010]星际竞速

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 2051  Solved: 1263[Submit][Stat ...

  4. BZOJ 1927 星际竞速(最小费用最大流)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1927 题意:一个图,n个点.对于给出的每条边 u,v,w,表示u和v中编号小的那个到编号 ...

  5. BZOJ 1927: [Sdoi2010]星际竞速 费用流

    1927: [Sdoi2010]星际竞速 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  6. bzoj 1927 [Sdoi2010]星际竞速(最小费用最大流)

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1576  Solved: 954[Submit][Statu ...

  7. BZOJ 1927 星际竞速

    http://www.lydsy.com/JudgeOnline/problem.php?id=1927 思路:把一个点拆成两个点, S->i 费用0,流量1 (代表这个点可以移动到其他点所必备 ...

  8. BZOJ 1927: [Sdoi2010]星际竞速(最小费用最大流)

    拆点,费用流... ----------------------------------------------------------------------------- #include< ...

  9. BZOJ 1927: [Sdoi2010]星际竞速 [上下界费用流]

    1927: [Sdoi2010]星际竞速 题意:一个带权DAG,每个点恰好经过一次,每个点有曲速移动到他的代价,求最小花费 不动脑子直接上上下界费用流过了... s到点连边边权为曲速的代价,一个曲速移 ...

随机推荐

  1. vagrant简单学习使用

    1.安装vagrant 旧版本的vagrant可以在http://downloads.vagrantup.com/下载,支持的系统平台有mac,debian/ubuntu, centos,window ...

  2. PAT L1-044 稳赢

    https://pintia.cn/problem-sets/994805046380707840/problems/994805086365007872 大家应该都会玩“锤子剪刀布”的游戏:两人同时 ...

  3. python爬虫从入门到放弃(五)之 正则的基本使用(转)

    什么是正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是 事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符”,这个“规则字符” 来表达对字符的一种过滤逻辑. 正则并不是pyth ...

  4. input 元素 相对父元素错位

    <div class="recommend"> <i class="iconfont icon-user"></i> < ...

  5. POJ3630:Phone List——题解

    http://poj.org/problem?id=3630 简单的trie树问题,先添加,然后每个跑一边看中途有没有被打上结束标记即可. #include<cstdio> #includ ...

  6. BZOJ3675 & 洛谷3648 & UOJ104:[Apio2014]序列分割——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3675 https://www.luogu.org/problemnew/show/P3648 ht ...

  7. BZOJ3998:[TJOI2015]弦论——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3998 https://www.luogu.org/problemnew/show/P3975 对于 ...

  8. poj3177 BZOJ1718 Redundant Paths

    Description: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另 ...

  9. pandas模块(数据分析)------dataframe

    DataFrame DataFrame是一个表格型的数据结构,含有一组有序的列,是一个二维结构. DataFrame可以被看做是由Series组成的字典,并且共用一个索引. 一.生成方式 import ...

  10. 【状压DP】【P3959】【NOIP2017D2T2】宝藏

    Description 参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 $n$ 个深埋在地下的宝藏屋, 也给出了这 $n$ 个宝藏屋之间可供开发的$ m$ 条道路和它们的长度. 小明决心亲自前往挖 ...