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来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:梦游的龙猫(转 ...
随机推荐
- nodejs supvisor模块
在测试nodejs程序的时候,每次都需要在控制台编译,非常的麻烦.supervisor是一款无需重复手动编译,自动后台监听文件变化来自动编译,并且不需要在项目内require,使用非常的方便. 使用方 ...
- SPOJ FIBOSUM && FIBOSUM2
Fibonacci数列定义为 $$f_n = f_{n-1}+f_{n-2}, \text{以及初值}f_0=0, f_1=1.$$ 本文之讨论,皆在模$10^9+7$意义下. FIBOSUM 给定$ ...
- View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views
Views Because view objects are the main way your application interacts with the user, they have many ...
- teamviewer被识别为商业用途
1.卸载teamviewer,在控制面板里或者用360等软件卸载: 2.删除下面两个目录 C:\Program Files (x86)\TeamViewer C:\Users\Administrato ...
- 更新gitignore
更新: 2017/04/26 修正windows版本下的命令 git rm -r --cached . (Windows 下的版本) 更新: 2017/06/06 mac下的命令也 ...
- hdoj1007【几何】【未完待续】
题意: 在一个平面上有n(1e5)个点,然后求一个圆来包住这些点,求这个圆的最小半径. 思考: 要使一个圆直接包了这些点,没有任何思路..
- rpm -e ** error :No such file or directory 解决
参考文章:http://www.redhat.com/archives/rpm-list/2006-June/msg00025.html 我遇到的情况是这样的: 1 先安装包 rpm -ivh tes ...
- 第十七篇 .NET高级技术之XML
Xml 简介(可扩展标记语言) XML优点:容易读懂:格式标准任何语言都内置了XML分析引擎,不用单独进行文件分析引擎的编写. Xml就是用一种格式化的方式来存储数据,我们可以通过用记事本打开. .n ...
- Selenium | 基础入门 | 利用Xpath寻找用户框
以微信公众号登陆界面为例, 找到相对应的Xpath的方法, 核心代码: System.setProperty(“webdriver.chrome.driver”,“ C:\\Program Files ...
- Qt基本应用
1 使用方式 在qt designer中直接设计图形界面,然后使用pyGUI转换成py文件. 可以发现,转换的文件为一个class.并不是一个完整的程序(运行时无法出现窗口).这个类名字是Ui_Mai ...