uva12433 Rent a Car
init 一开始搞成2*n+2了...囧 所以初始化很重要!
然后提交的时候忘了删调试的数据了。。囧
技巧:设立虚拟节点
建图比较麻烦(非常)。
要考虑到保养完了的车可以免费再用
设立S,T ,1-N, N+1-2N
S连1-N,cap为ri,cost为0,表示用了的还没维护的车,还要链i,i+1
T连N+1 --2N,cap为ri cost为0 表示目标
设立虚拟节点2*n+1,S连之,cap为ci,cost为pi,2*n+1再连到N+1 -- N,cap为ri,
表示可以花钱买
if(i+d[j]+1<=n) addedge(i,i+d[j]+1+n,INF,s[j]); 表示可以免费得到的维护过车
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
int r[],c[],p[],d[],s[];
struct Edge {
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w):from(u),to(v),cap(c),flow(f),cost(w) {}
}; struct MCMF {
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn]; // 是否在队列中
int d[maxn]; // Bellman-Ford
int p[maxn]; // 上一条弧
int a[maxn]; // 可改进量 void init(int n) {
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.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 BellmanFord(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];
for(int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
}
return true;
} // 需要保证初始网络中没有负权圈
int MincostFlow(int s, int t,int& cost) {
int flow = ; cost = ;
while(BellmanFord(s, t,flow, cost)); return flow;
} }; MCMF g; int main ()
{
int u,j,T,C,R,ans,n;
scanf("%d",&T);
for(int kase=; kase<=T; kase++)
{
ans=;
int cnt=;
scanf("%d%d%d",&n,&C,&R);
g.init(*n+); //init 一开始搞成2*n+2了...囧
for(int i=; i<=n; i++)
scanf("%d",&r[i]),ans+=r[i];
for(int i=; i<=C; i++)
scanf("%d%d",&c[i],&p[i]);
for(int i=; i<=R; i++)
scanf("%d%d",&d[i],&s[i]); for(int i=; i<=C; i++)
g.Addedge(,*n+,c[i],p[i]);
for(int i=; i<n; i++)
g.Addedge(i,i+,INF,); for(int i=; i<=n; i++)
g.Addedge(,i,r[i],);
for(int i=n+; i<=*n; i++)
g.Addedge(i,(n+)*,r[i-n],);
for(int i=n+; i<=*n; i++)
g.Addedge(*n+,i,INF,);
for(int i=; i<=n; i++)
{
for(int j=; j<=R; j++)
{
if(i+d[j]+<=n) g.Addedge(i,i+d[j]++n,INF,s[j]);
}
}
int cos;
int f=g.MincostFlow(,*n+,cos); printf("Case %d: ",kase);
if(f!=ans)
printf("impossible\n");
else
printf("%d\n",cos);
}
return ;
}
/*
2
3 2 1
10 20 30
40 90 15 100
1 5
*/
uva12433 Rent a Car的更多相关文章
- UVA12433 【Rent a Car】
这题应该算是比较难的一道网络流的题,(但却在我校OJ考试上出现了),但是大家只要能理解此图的建边方式就行. 假设有5天的租车需求,虚拟出2*n+2 即 12个节点,0为源点,12为汇点. 1,源点到1 ...
- rent bike问题(二分+贪心)
题目描述: A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to ...
- rent a apartment
今日焦点 month-to-month 按月 6-month minimum 至少六个月 sublease 转租 drenched in sunlight 阳光充足的 词汇实践 I am lookin ...
- SPOJ RENT 01背包的活用+二分
这个题目给定N航班的发出时间和结束时间以及价值,要求不冲突时间的最大价值 第一时间想到经典的N方DP,即对航班按发出时间排一下序之后每个i对前面的都扫一遍 时间过不了N有10万,只能想优化了,一开始想 ...
- SPOJ 130 - Rent your airplane and make money(dp+优化)
题意:有n列预定航班,从st时刻开始出发,飞行时间为d,花费为p,且同一时刻不能有两个航班,求最大的花费 对航班的开始时间(或结束时间)按升序排序,从后往前找到对应结束时间所在的航班位置(如按结束时间 ...
- 用C++实现Linux中shell的ls功能
实现输出当前目录下的文件名 ls功能: 方法一: #include <iostream> #include <algorithm> #include <stdio.h&g ...
- 设计模式-代理模式(Proxy Model)
文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/5988145.html 目录 1.写在前面 2.概述 3.目的 4.结构组成 5.实现 5 ...
- 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(前言)
一直希望能够搭建一个完整的,基础Web框架,方便日后接一些外快的时候,能够省时省力,终于花了一周的时间,把这个东西搞定了.特此写下此博客,一来是纪念,二来是希望能够为别人提供方便.顺带说一下,恩,组合 ...
- 【Bootstrap-插件使用】Jcrop+fileinput组合实现头像上传功能
作者:Dreawer链接:https://zhuanlan.zhihu.com/p/24465742来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:梦游的龙猫(转 ...
随机推荐
- uoj103 apio2014 Palindromes
题目链接:http://uoj.ac/problem/103 题解: 首先,我们可以用后缀自动机算出每个字符串的出现次数.然后我们可以用manacher找出所有不同的回文串(o(n)个),统计答案即可 ...
- bzoj1925地精部落——数学
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1925 真是精妙的递推式...好难想到啊: 详见这位的博客:https://www.cnblo ...
- linux设备驱动第三篇:如何实现一个简单的字符设备驱动
在linux设备驱动第一篇:设备驱动程序简介中简单介绍了字符驱动,本篇简单介绍如何写一个简单的字符设备驱动.本篇借鉴LDD中的源码,实现一个与硬件设备无关的字符设备驱动,仅仅操作从内核中分配的一些内存 ...
- 修改CentOS系统的默认启动级别
======修改CentOS系统的默认启动级别====== 现在的Linux系统安装完后就运行在第5个级别,即系统启动后直接进入图形界面,而不用在字符模式下登录后用startx或者xinit来起动图形 ...
- 洛谷 - P1361 - 小M的作物 - 最小割 - 最大权闭合子图
第一次做最小割,不是很理解. https://www.luogu.org/problemnew/show/P1361 要把东西分进两类里,好像可以应用最小割的模板,其中一类A作为源点,另一类B作为汇点 ...
- 洛谷 - P1309 - 瑞士轮 - 归并排序
https://www.luogu.org/problemnew/show/P1309 一开始写的直接快排没想到真的TLE了. 想到每次比赛每个人前移的量不会很多,但是不知从哪里开始优化. 搜索一下原 ...
- python __builtins__ classmethod类 (11)
11.'classmethod', 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等. class cla ...
- TCP协议 三次握手四次挥手
当某个应用端想基于TCP协议与另一个应用端通信时,它会发送一个通信请求. 这个请求必须被送到一个确切的地址.在双方“握手”之后,TCP 将在两个应用程序之间建立一个全双工 (full-duplex) ...
- Springboot 配置 application.yml 连接MySQL数据库
1.在pom.xml的<dependencies></dependencies>标签中中加入以下依赖 <dependency> <groupId>org ...
- 手机测试用例-wap测试用例
Software Test Case P/F comment tester test time P/F comment tester ID 功能描述 操作步骤 预期结果 备注 wap_001 wap ...