http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5552

输入n,表示有n个数1到n。A先拿,B后拿,依次拿,每次可以拿任意一个数,同时会删去这个数的所有因子,最后谁没得拿了谁输。

解法:推了前几个,0,a输,别的a都能赢,证明没想,猜过去的。

网上一个人说的,也不是很清晰:“如果先取的在2-n中取必输,则先取1, 
否则则在2-n中取,同时会把1取走,必赢”

 #include<cstdio>
int main(){
int n;
while(~scanf("%d",&n)){
puts(n?"win":"fail");
}
return ;
}

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5555

n点m边无向图,每个点原来有ai,每个点最终需要bi,有边连就可以把任意个物品移动过去,移动一个物品的花费是1.

解法:最小费用最大流,比赛时建图如下

图中每个点拆成两个点u,u*,源点s连接每一个u,流量是ai,费用为0,ui 到 ui * 流量inf,费用为0, 输入的边在u之间建双向边,流量inf,费用1.

每一个 ui* 连汇点t,流量bi,费用0.

最后最大流要是sum of bi说明存在解,否则-1. 费用只会产生在u之间的边,最小费用就是这个题的答案。

 #include<cstdio>
#include<cstring>
#include<queue>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int M=;
class MaxFlowMinCost { ///最小费用最大流 o(ME)
typedef int typef;///流量的类型
typedef int typec;///费用的类型
static const int ME=2e6+;///边的个数
static const int MV=2e2+;///点的个数
queue<int> q;
int cur[MV],pre[MV];
bool used[MV],sign[MV];
typef flow;
typec cost,dist[MV];
bool spfa(int s,int t) {
mt(used,);
mt(sign,);
mt(dist,);
used[s]=sign[s]=true;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty()) {
int u=q.front();
q.pop();
used[u]=false;
for(int i=g.head[u]; ~i; i=g.e[i].next) {
if(g.e[i].flow<) continue;
int v=g.e[i].v;
typec c=g.e[i].cost;
if(!sign[v]||dist[v]>dist[u]+c) {
dist[v]=dist[u]+c;
sign[v]=true; pre[v]=u;
cur[v]=i;
if(used[v]) continue;
used[v]=true;
q.push(v);
}
}
}
return sign[t];
} struct G {
struct E {
int v,next;
typef flow;
typec cost;
} e[ME];
int le,head[MV];
void init() {
le=;
mt(head,-);
} void add(int u,int v,typef flow,typec cost) {
e[le].v=v;
e[le].flow=flow;
e[le].cost=cost;
e[le].next=head[u];
head[u]=le++;
}
} g;
public:
void init() {
g.init();
} void add(int u,int v,typef flow,typec cost) {
g.add(u,v,flow,cost);
g.add(v,u,,-cost);
} void solve(int s,int t) {
flow=cost=;
while(spfa(s,t)) {
int temp=t;
typef now=inf;
while(temp!=s) {
now=min(now,g.e[cur[temp]].flow); temp=pre[temp];
}
flow+=now;
temp=t;
while(temp!=s) {
int id=cur[temp];
cost+=now*g.e[id].cost;
g.e[id].flow-=now;
g.e[id^].flow+=now;
temp=pre[temp];
}
}
} typef getflow() {
return flow;
} typec getcost() {
return cost;
}
}mfmc;
int a[M],b[M];
int main() {
int n,m,u,v;
while(~scanf("%d%d",&n,&m)) {
for(int i=; i<=n; i++) {
scanf("%d%d",&a[i],&b[i]);
}
mfmc.init();
int s=n+n+;
int t=s+;
int sum=;
for(int i=;i<=n;i++){
mfmc.add(s,i,a[i],);
mfmc.add(i+n,t,b[i],);
mfmc.add(i,i+n,inf,);
sum+=b[i];
}
while(m--) {
scanf("%d%d",&u,&v);
mfmc.add(u,v,inf,);
mfmc.add(v,u,inf,);
}
mfmc.solve(s,t);
int ans=-;
if(sum==mfmc.getflow()){
ans=mfmc.getcost();
}
printf("%d\n",ans);
}
return ;
}

后来看网上有个建图更好,不用拆点,想想也是,中间流量inf,费用0,那些就直接流过去了,我们可以在脑子把他跑掉,也就是我们只需要对n个点建图,对于输入的边,还是在uv之间建流量inf费用1的双向边,对于a》b的情况,从源点到u连a-b的流,费用0,对于a《b的情况,从u连到汇点t,流量b-a,。a==b的不用连了,这样建图的贪心的默认自己留给自己是最好的,想想也是挺对的。源点出来的流相当于每个点可提供的,流向汇点的相当于每个点的需求。

 #include<cstdio>
#include<cstring>
#include<queue>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int M=;
class MaxFlowMinCost { ///最小费用最大流 o(ME)
typedef int typef;///流量的类型
typedef int typec;///费用的类型
static const int ME=6e2+;///边的个数
static const int MV=1e2+;///点的个数
queue<int> q;
int cur[MV],pre[MV];
bool used[MV],sign[MV];
typef flow;
typec cost,dist[MV];
bool spfa(int s,int t) {
mt(used,);
mt(sign,);
mt(dist,);
used[s]=sign[s]=true;
while(!q.empty()) q.pop();
q.push(s);
while(!q.empty()) {
int u=q.front();
q.pop();
used[u]=false;
for(int i=g.head[u]; ~i; i=g.e[i].next) {
if(g.e[i].flow<) continue;
int v=g.e[i].v;
typec c=g.e[i].cost;
if(!sign[v]||dist[v]>dist[u]+c) {
dist[v]=dist[u]+c;
sign[v]=true; pre[v]=u;
cur[v]=i;
if(used[v]) continue;
used[v]=true;
q.push(v);
}
}
}
return sign[t];
} struct G {
struct E {
int v,next;
typef flow;
typec cost;
} e[ME];
int le,head[MV];
void init() {
le=;
mt(head,-);
} void add(int u,int v,typef flow,typec cost) {
e[le].v=v;
e[le].flow=flow;
e[le].cost=cost;
e[le].next=head[u];
head[u]=le++;
}
} g;
public:
void init() {
g.init();
} void add(int u,int v,typef flow,typec cost) {
g.add(u,v,flow,cost);
g.add(v,u,,-cost);
} void solve(int s,int t) {
flow=cost=;
while(spfa(s,t)) {
int temp=t;
typef now=inf;
while(temp!=s) {
now=min(now,g.e[cur[temp]].flow); temp=pre[temp];
}
flow+=now;
temp=t;
while(temp!=s) {
int id=cur[temp];
cost+=now*g.e[id].cost;
g.e[id].flow-=now;
g.e[id^].flow+=now;
temp=pre[temp];
}
}
} typef getflow() {
return flow;
} typec getcost() {
return cost;
}
}mfmc;
int a[M],b[M];
int main() {
int n,m,u,v;
while(~scanf("%d%d",&n,&m)) {
for(int i=; i<=n; i++) {
scanf("%d%d",&a[i],&b[i]);
}
mfmc.init();
int s=;
int t=n+;
int sum=;
for(int i=;i<=n;i++){
if(a[i]>b[i]){
mfmc.add(s,i,a[i]-b[i],);
}
else if(a[i]<b[i]){
mfmc.add(i,t,b[i]-a[i],);
sum+=b[i]-a[i];
}
}
while(m--) {
scanf("%d%d",&u,&v);
mfmc.add(u,v,inf,);
mfmc.add(v,u,inf,);
}
mfmc.solve(s,t);
int ans=-;
if(sum==mfmc.getflow()){
ans=mfmc.getcost();
}
printf("%d\n",ans);
}
return ;
}

end

ZOJ Monthly, July 2015的更多相关文章

  1. 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys

    题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...

  2. Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)

    Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal ...

  3. ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H

    Bob wants to pour water Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge There i ...

  4. ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  5. ZOJ 3910 Market ZOJ Monthly, October 2015 - H

    Market Time Limit: 2 Seconds      Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...

  6. ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F

    Number Game Time Limit: 2 Seconds      Memory Limit: 65536 KB The bored Bob is playing a number game ...

  7. ZOJ 3905 Cake ZOJ Monthly, October 2015 - C

    Cake Time Limit: 4 Seconds      Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...

  8. ZOJ 3903 Ant ZOJ Monthly, October 2015 - A

    Ant Time Limit: 1 Second      Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...

  9. matrix_2015_1 138 - ZOJ Monthly, January 2015

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3844 第一个,n个数,每次操作最大数和最小数都变成他们的差值,最后n个数相 ...

随机推荐

  1. PopupWindow的简单使用

    测试代码: package com.zzw.testpopuwindows; import android.app.Activity; import android.graphics.Color; i ...

  2. java android 中的Toast

    package com.example.my1; import android.os.Bundle;import android.app.Activity;import android.content ...

  3. Sorl之.net操作

    http://www.cnblogs.com/zhangweizhong/category/771055.html 插入: SolrNet.Startup.Init<Movie>(&quo ...

  4. UCOS2_STM32F1移植详细过程(四)

    Ⅰ.概述 上一篇文章是讲述uC/OS-II Ports下面os_cpu_a.asm.os_cpu_c.c和os_cpu.h文件底层端口代码的移植(修改)和说明,接着上一篇文章来讲述关于UCOS移植应用 ...

  5. 4.html5中超链接

    html中超链接都是通过<a>标签实现的,html5也不例外,这里就来探讨一下<a>标签. <a>元素属于文本元素,有一些私有属性或者叫局部属性.那么,相对应的还有 ...

  6. Python核心编程--学习笔记--7--字典和集合

    本章介绍Python语言中的映射类型(字典)和集合类型,包括操作符.工厂函数.内建函数与方法. 1 字典 字典是Python中唯一的映射类型——键key直接映射到值value.字典是容器类型,其对象是 ...

  7. Python学习教程(learning Python)--2.2 Python下的变量基础

    变量的基本概念,变量可以这样去理解,变量是一个值,这个值存储在计算机的内存里.以 网购为例,您在选购傻商品的时候,是在不同页面里选不同的商品,选好一件点击“放入购物车”,选完了再点击去结帐,这些商品的 ...

  8. Ruby on Rail学习笔记

    说明:只针对Windows8.1 Windows下,上rubyinstaller上下载最新的railsinstaller包含Ruby2.1的,然后更新gem 用命令: gem update --sys ...

  9. SaaS应用“正益工作”发布,为大中型企业轻松构建移动门户

    6月24日,以“平台之上,应用无限”为主题的2016 AppCan移动开发者大会,在北京国际会议中心隆重举行,逾1500名移动开发者一起见证了此次大会盛况. 会上,在专家领导.技术大咖.移动开发者的共 ...

  10. Convert Geometry data into a Geography data in MS SQL Server

    DECLARE @geog GEOGRAPHY; DECLARE @geom GEOMETRY; ); SET @geom = @geom.MakeValid() --Force to valid g ...