首先我们可以知道这道题中每个点只能经过一次,那么我们引入附加源汇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. windows与linux下执行.class(包含main方法)

    来源:http://blog.csdn.net/hanqunfeng/article/details/4327325 一般来说,执行一个java文件采用执行jar包的方式最为方便(java -jar ...

  2. JAVA学习之HashCode

    public native int hashCode(); 返回该对象的哈希码值.支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能. 一.HashCode ...

  3. hdu 2108 Shape of HDU (数学)

    Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. 51nod 1304 字符串的相似度(exkmp)

    拓展kmp裸题 自己跟自己匹配即可 模板测试=v= #include <iostream> #include <cstring> using namespace std; ; ...

  5. [洛谷P5205]【模板】多项式开根

    题目大意:给你$n$项多项式$A(x)$,求出$B(x)$满足$B^2(x)\equiv A(x)\pmod{x^n}$ 题解:考虑已经求出$B_0(x)$满足$B_0^2(x)\equiv A(x) ...

  6. 【SPOJ - GSS2】Can you answer these queries II(线段树)

    区间连续不重复子段最大值,要维护历史的最大值和当前的最大值,打两个lazy,离线 #include<cstdio> #include<cstring> #include< ...

  7. cf1073d Berland Fair

    ~~~题面~~~ 题解: 可以发现,每走完一圈付的钱和买的数量是有周期性的,即如果没有因为缺钱而买不起某家店的东西,那么这一圈的所以决策将会和上一圈相同,这个应该是很好理解的,想想就好了. 因为钱数固 ...

  8. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  9. apache出现You don't have permission to access / on this server. 提示

    今天在新的linux上跑原来的代码,使用的虚拟主机的模式进行操作.几个相关的网站放在一个文件里,想法是通过网站列出的目录进行相应的网站进行操作.一切设置完成后,在浏览器中运行出现在You don't ...

  10. Linux系统上的popen()库函数

    popen可以是系统命令,也可以是自己写的程序a.out. 假如a.out就是打印 “hello world“ 在代码中,想获取什么,都可以通过popen获取. 比如获取ls的信息, 比如获取自己写的 ...