http://www.cnblogs.com/jianglangcaijin/archive/2012/09/24/2700509.html

思路:

S->0 流量为K费用0

0->i 流量为inf,费用为a[0][i]

0->T 流量为K,费用0

i->i+n 流量为1,费用为-inf

i+n->T 流量为1,费用为a[0][i]

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#define inf 1000000
int a[][],K;
int tot,go[],next[],first[],cost[],flow[];
int op[],dis[],c[],vis[],edge[],from[];
int S,T,n,m,ans;
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y,int z,int l){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
flow[tot]=z;
cost[tot]=l;
}
void add(int x,int y,int z,int l){
insert(x,y,z,l);op[tot]=tot+;
insert(y,x,,-l);op[tot]=tot-;
}
bool spfa(){
for (int i=;i<=T;i++)
dis[i]=0x3f3f3f3f,vis[i]=;
int h=,t=;c[]=S;vis[S]=;dis[S]=;
while (h<=t){
int now=c[h++];
for (int i=first[now];i;i=next[i]){
int pur=go[i];
if (dis[pur]>dis[now]+cost[i]&&flow[i]){
dis[pur]=dis[now]+cost[i];
edge[pur]=i;
from[pur]=now;
if (vis[pur]) continue;
vis[pur]=;
c[++t]=pur;
}
}
vis[now]=;
}
return dis[T]!=0x3f3f3f3f;
}
void updata(){
int mn=0x3f3f3f3f;
for (int i=T;i!=S;i=from[i]){
mn=std::min(mn,flow[edge[i]]);
}
for (int i=T;i!=S;i=from[i]){
ans+=mn*cost[edge[i]];
flow[edge[i]]-=mn;
flow[op[edge[i]]]+=mn;
}
}
int main(){
while (scanf("%d",&n)!=EOF){
m=read();K=read();
if (n==&&K==&&m==) return ;
for (int i=;i<=T;i++) first[i]=;tot=;
S=*n+;T=S+;
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (i==j) a[i][j]=;
else a[i][j]=0x3f3f3f3f;
while(m--){
int u=read(),v=read(),w=read();
a[u][v]=std::min(a[u][v],w);
a[v][u]=std::min(a[v][u],w);
}
for (int k=;k<=n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
a[i][j]=std::min(a[i][j],a[i][k]+a[k][j]);
add(S,,K,);
add(,T,K,);
for (int i=;i<=n;i++)
if (a[][i]<0x3f3f3f3f)
add(,i,inf,a[][i]);
for (int i=;i<=n;i++)
for (int j=i+;j<=n;j++)
if (a[i][j]<0x3f3f3f3f)
add(i+n,j,,a[i][j]);
for (int i=;i<=n;i++)
if (a[][i]<0x3f3f3f3f)
add(i+n,T,,a[][i]);
for (int i=;i<=n;i++)
add(i,i+n,,-inf);
ans=inf*n;
while (spfa()) updata();
printf("%d\n",ans);
}
}

HDU 4411 Arrest的更多相关文章

  1. hdu 4411 arrest 最小费用流

    #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #i ...

  2. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

  3. hdu 4411 最小费用流

    思路:主要就是要把一个每个城市拆为两个点,建一条容量为1,费用为-inf的边,保证每个城市都会被遍历. /*最小费用最大流*/ #include<iostream> #include< ...

  4. [GodLove]Wine93 Tarining Round #7

    比赛链接: http://vjudge.net/contest/view.action?cid=47643#overview 比赛来源: 2012 ACM/ICPC Asia Regional Han ...

  5. (KMP 扩展)Clairewd’s message -- hdu -- 4300

    http://acm.hdu.edu.cn/showproblem.php?pid=4300 Clairewd’s message Time Limit: 2000/1000 MS (Java/Oth ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. Delphi - GetUserNameEx(学一下导出Windows API,以及Array Char充当缓冲区的用法,下标必须从零开始)

    (* * Author : http://www.michael-puff.de * Date : 2006-03-29 * License : PUBLIC DOMAIN *) function G ...

  2. 使用kthread内核线程的内核模块

    这里使用了msleep(50); printk 开启其实挺大的,当我使用msleep(10);机器直接卡死了: 另外ISERR不能判断结构体的,只能判断 空指针 #cat hello.c #inclu ...

  3. python scrapy 基础

    scrapy是用python写的一个库,使用它可以方便的抓取网页. 主页地址http://scrapy.org/ 文档 http://doc.scrapy.org/en/latest/index.ht ...

  4. Android软键盘弹出时布局问题

    最近项目需要做一个类似聊天室的模块,基于Socket实现的,这部分稍后一段时间再做总结,功能上的相关点都实现了小例子也做出来了,最后发现一个比较腻歪的问题就是软键盘弹出时总是会把标题“挤出”屏幕,(无 ...

  5. IE常见的CSS的BUG(一)

    2011年6月,我毕业了.2012年我接触了CSS,本以为会好过些能赚点钱了,可谁知,Internet Explorer(下称IE),这个前端工程师的噩梦浏览器让我不再那么好过了.各种出现在IE身上的 ...

  6. Difference between enabled and userInteractionEnabled properties

    I read through the documentation, and here are my findings. UIButton inherits from UIControl the boo ...

  7. Android 编程下 Activity 的创建和应用退出时的销毁

    为了确保对应用中 Activity 的创建和销毁状态进行控制,所以就需要一个全局的变量来记录和销毁这些 Activity.这里的大概思路是写一个类继承 Application,并使获取该 Applic ...

  8. WebApi2官网学习记录---Attribute Routing

    从WebApi 1迁移到WebAPI 2要改变配置代码如下: WebApi 1: protected void Application_Start() { // WARNING - Not compa ...

  9. c#转码解码

    ///反转码                          mdata[k].MNAME = unescape(mdata[k].MNAME);程家楠 13:51:00 Microsoft.JSc ...

  10. oracle插入例子

    string sql = "insert into EMST_JC_SBXX(XL,SBBM,SBWH,SBMC,CCBM,XNCS,CZXL,ZL,GL,ZZCJ,TCRQ,SYQX,XH ...