NWERC 2012 Problem A Admiral
一个最小费用最大流的简单建模题;
比赛的时候和小珺合力想到了这个题目的模型;
方法:拆点+边的容量为1
这样就可以保证他们不会在点上和边上相遇了!
感谢刘汝佳大神的模板,让我这个网络流的小白A了这个题。
代码:
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define maxn 42005
#define inf 99999
using namespace std; struct edge
{
int from ,to,cap,flow,cost;
}; struct mcmf
{
int n,m,s,t;
vector<edge>edges;
vector<int>g[maxn];
int inq[maxn],d[maxn],p[maxn],a[maxn];
void init(int n)
{
this->n=n;
edges.clear();
for(int i=; i<n; i++)g[i].clear();
}
void addedge(int from,int to,int cap,int cost)
{
edges.push_back((edge){from,to,cap,,cost});
edges.push_back((edge){to,from,,,-cost});
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool bellmamford(int s,int t,int &flow,int& cost)
{
for(int i=; i<n; i++)d[i]=inf;
memset(inq,,sizeof inq);
d[s]=;
inq[s]=;
p[s]=;
a[s]=inf;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=;
for(int i=; i<g[u].size(); i++)
{
edge& e=edges[g[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost)
{
d[e.to]=d[u]+e.cost;
p[e.to]=g[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to])
{
q.push(e.to);
inq[e.to]=;
}
}
}
}
if(d[t]==inf)return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s)
{
edges[p[u]].flow+=a[t];
edges[p[u]^].flow-=a[t];
u=edges[p[u]].from;
}
return true;
} int mincost(int s,int t)
{
int flow=,cost=;
while(bellmamford(s,t,flow,cost));
return cost;
}
}getans; int main()
{
int nn,mm,f,t,c;
while(scanf("%d%d",&nn,&mm)!=EOF)
{
getans.init(*nn+);
getans.addedge(,+nn,,);
getans.addedge(nn,*nn+,,);
for(int i=;i<nn;i++)
getans.addedge(i,i+nn,,);
for(int i=; i<mm; i++)
{
scanf("%d%d%d",&f,&t,&c);
getans.addedge(f+nn,t,,c);
}
printf("%d\n",getans.mincost(,*nn+));
}
return ;
}
NWERC 2012 Problem A Admiral的更多相关文章
- NWERC 2012 Problem J Joint Venture
刚刚开始想的是用二分的方法做,没想到这个题目这么水,直接暴力就行: 代码: #include<cstdio> #include<algorithm> #define maxn ...
- NWERC 2012 Problem I Idol
又是个2-sat的模板题: 反正评委的选择必须有一个是正确的,1错误,那么2就必须正确: 这就是一个2-sat问题. 直接上白书的模板啊,不过稍微要注意的一点是对于第一个点必须要选择,不然就违反了题意 ...
- NWERC 2012 Problem E Edge Case
比赛的时候刷了一点小聪明,发现这个数列是卢卡斯数,一个递推关系像斐波拉契数列的数列: 我不知道怎么证明,如果哪天无意中会证了再加上: 这题唯一的难点就是大数运算: 直接用JAVA 代码: import ...
- Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights
一个简单的题: 感觉像计算几何,其实并用不到什么计算几何的知识: 方法: 首先对每条边判断一下,看他们能够把平面分成多少份: 然后用边来对点划分集合,首先初始化为一个集合: 最后如果点的集合等于平面的 ...
- Central Europe Regional Contest 2012 Problem c: Chemist’s vows
字符串处理的题目: 学习了一下string类的一些用法: 这个代码花的时间很长,其实可以更加优化: 代码: #include<iostream> #include<string> ...
- Central Europe Regional Contest 2012 Problem J: Conservation
题目不难,感觉像是一个拓扑排序,要用双端队列来维护: 要注意细节,不然WA到死 = =! #include<cstdio> #include<cstring> #includ ...
- Central Europe Regional Contest 2012 Problem H: Darts
http://acm.hunnu.edu.cn/online/problem_pdf/CERC2012/H.pdf HUNNU11377 题意:飞镖环有十个环,没个环从外到里对应一个得分1~10,每个 ...
- 2012-2013 Northwestern European Regional Contest (NWERC 2012)
B - Beer Pressure \(dp(t, p_1, p_2, p_3, p_4)\)表示总人数为\(t\),\(p_i\)对应酒吧投票人数的概率. 使用滚动数组优化掉一维空间. 总的时间复杂 ...
- HDU 2012 素数判定
http://acm.hdu.edu.cn/showproblem.php?pid=2012 Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括 ...
随机推荐
- Kafka 0.8: 多日志文件夹机制
kafka 0.7.2 中对log.dir的定义如下: log.dir none Specifies the root directory in which all log data is kept. ...
- NIO学习:buffer读入与写出(文件复制示例)
FileInputStream fInputStream=new FileInputStream(new File("/root/Desktop/testImage.jpg")); ...
- 亿能测试白盒安全测试模板V1.0发布
亿能测试白盒安全测试模板V1.0发布http://automationqa.com/forum.php?mod=viewthread&tid=2911&fromuid=21
- FTP上传下载
使用的是apache开源包commons-net-3.3.jar所提供的FTPClient FTP服务器使用Quick Easy FTP Server 4.0.0(服务器ip为192.168.31.1 ...
- 通过js实时检测文本框内容
思路 1,在获取文本框焦点后,启动定时器,每隔100毫秒来查看文本内容的改变 2,在文本框失去焦点后,清除定时器 下面是一个简单的例子 <!DOCTYPE html> <html&g ...
- java struts2自定义调用方法
一个action里面不只会调用一个方法,肯定会用到其他的方法,也写在同一个action里面. 这里不重点讲解了,就直接上代码 struts.xml <?xml version="1.0 ...
- Top 10 Uses of a Message Queue
Top 10 Uses of a Message QueueAsynchronicity, Work Dispatch, Load Buffering, Database Offloading, an ...
- C# HTTP 请求
public class HttpHelper { /// <summary> /// 创建GET方式的HTTP请求 /// </summary> public static ...
- 一步一步学NUnit
转载:http://tech.sina.com.cn/s/2009-07-17/1129988785.shtml 单元测试基础知识 单元测试是开发者编写的一小段代码,用于检验被测代码的一个很小的.很明 ...
- poj1182 食物链(种类并查集)详解
poj 1182 http://poj.org/problem?id=1182 分析:这个题大意说的非常清楚了,就是求出假话的个数,题目中给的假话要求有三个 ① 当前的话与前面的某些真的话冲突,是 ...