题解:最小费用流+二分图模型;

左边表示出这个点,右边表示入这个点;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=10009;
const int inf=1000000000; int n,m;
int a[maxn];
struct Edge{
int from,to,cap,flow,cost;
};
vector<int>G[maxn];
vector<Edge>edges;
int addedge(int x,int y,int z,int w){
Edge e;
e.from=x;e.to=y;e.cap=z;e.flow=0;e.cost=w;
edges.push_back(e);
e.from=y;e.to=x;e.cap=0;e.flow=0;e.cost=-w;
edges.push_back(e);
int c=edges.size();
G[x].push_back(c-2);
G[y].push_back(c-1);
} int s,t,totn;
queue<int>q;
int inq[maxn];
int d[maxn];
int p[maxn];
int Spfa(int &nowflow,int &nowcost){
for(int i=1;i<=totn;++i){
d[i]=inf;inq[i]=0;
}
q.push(s);inq[s]=1;d[s]=0;p[s]=0;
while(!q.empty()){
int x=q.front();q.pop();inq[x]=0;
for(int i=0;i<G[x].size();++i){
Edge e=edges[G[x][i]];
if(e.cap>e.flow&&d[x]+e.cost<d[e.to]){
d[e.to]=d[x]+e.cost;
p[e.to]=G[x][i];
if(!inq[e.to]){
q.push(e.to);
inq[e.to]=1;
}
}
}
} if(d[t]==inf)return 0;
int f=inf,x=t;
while(x!=s){
Edge e=edges[p[x]];
f=min(f,e.cap-e.flow);
x=e.from;
}
nowflow+=f;nowcost+=f*d[t];
x=t;
while(x!=s){
edges[p[x]].flow+=f;
edges[p[x]^1].flow-=f;
x=edges[p[x]].from;
}
return 1;
} int Mcmf(){
int flow=0,cost=0;
while(Spfa(flow,cost));
return cost;
} int minit(){
edges.clear();
for(int i=1;i<=totn;++i)G[i].clear();
} int main(){
scanf("%d%d",&n,&m);
s=n+n+1;t=totn=s+1;minit(); for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
addedge(s,i+n,1,a[i]);
}
for(int i=1;i<=m;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(x>y)swap(x,y);
addedge(x,n+y,1,z);
}
for(int i=1;i<=n;++i){
addedge(s,i,1,0);
addedge(i+n,t,1,0);
}
printf("%d\n",Mcmf());
return 0;
}

  

BZOJ:1927: [Sdoi2010]星际竞速的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. BZOJ.1927.[SDOI2010]星际竞速(无源汇上下界费用流SPFA /最小路径覆盖)

    题目链接 上下界费用流: /* 每个点i恰好(最少+最多)经过一次->拆点(最多)+限制流量下界(i,i',[1,1],0)(最少) 然后无源汇可行流 不需要源汇. 注: SS只会连i',求SS ...

  9. bzoj 1927 [Sdoi2010]星际竞速【最小费用最大流】

    果然还是不会建图- 设\( i \)到\( j \)有通路,代价为\( w[i][j] \),瞬移到i代价为\( a[i] \),瞬移到i代价为\( a[j] \),逗号前是流量. 因为每个点只能经过 ...

  10. BZOJ 1927: [Sdoi2010]星际竞速(费用流)

    传送门 解题思路 仿照最小路径覆盖问题,用费用流解决此题.最小路径覆盖问题是拆点连边后用\(n-\)最大匹配,这里的话也是将每个点拆点,源点向入点连流量为\(1\),费用为\(0\)的边,向出点连流量 ...

随机推荐

  1. zabbix4.4安装 centos7+mysql+Nginx

    1.安装数据源 # rpm -Uvh https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch. ...

  2. ttf格式文件

    TTF(TrueTypeFont):是一种字库名称.TTF文件:是Apple公司和Microsoft公司共同推出的字体文件格式.要使用的下载的字体文件只要把它(*.ttf)放到C:\WINDOWS\F ...

  3. windows驱动不要签名

    BCDEDIT -SET LOADOPTIONS DISABLE_INTEGRITY_CHECKSBCDEDIT -SET TESTSIGNING ON

  4. SChema中group指示器的使用

    <?xml version="1.0" encoding="UTF-8"?> <!-- edited with XMLSpy v2011 (h ...

  5. Day6 - J - Cartesian Tree POJ - 2201

    Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binar ...

  6. 048、Java中使用switch判断

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  7. python SSTI tornado render模板注入

    原理tornado render是python中的一个渲染函数,也就是一种模板,通过调用的参数不同,生成不同的网页,如果用户对render内容可控,不仅可以注入XSS代码,而且还可以通过{{}}进行传 ...

  8. leetcode1 twoSum

    """ Given an array of integers, return indices of the two numbers such that they add ...

  9. B - Stacks of Flapjacks UVA - 120

    BackgroundStacks and Queues are often considered the bread and butter of data structures and find us ...

  10. 【剑指Offer】面试题27. 二叉树的镜像

    题目 请完成一个函数,输入一个二叉树,该函数输出它的镜像. 例如输入:      4    /   \   2     7  / \   / \ 1   3 6   9 镜像输出:      4   ...