Codeforces 1155F Delivery Oligopoly dp(看题解)
看别人写的才学会的。。。
我们考虑刚开始的一个点, 然后我们枚举接上去的一条一条链,
dp[mask]表示当前已经加进去点的状态是mask所需的最少边数。
反正就是很麻烦的一道题, 让我自己写我是写不出来的。。。 我好菜啊。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end() using namespace std; const int N = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e6 + ;
const int p = 1e6 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, m;
int G[][];
int vtomask[][ << ];
int dp[ << ], pre[ << ][];
vector<int> road[ << ][][];
vector<PII> ans; int main() {
memset(dp, inf, sizeof(dp));
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++) {
int u, v; scanf("%d%d", &u, &v);
u--; v--;
G[u][v] = G[v][u] = ;
}
for(int i = ; i < n; i++) {
for(int mask = ; mask < ( << n); mask++) {
for(int j = ; j < n; j++) {
if(mask >> j & ) {
if(G[i][j]) vtomask[i][mask]++;
}
}
}
}
for(int i = ; i < n; i++) road[ << i][i][i].push_back(i);
for(int mask = ; mask < ( << n); mask++) {
for(int u = ; u < n; u++) {
for(int v = ; v < n; v++) {
if(!SZ(road[mask][u][v])) continue;
for(int z = ; z < n; z++) {
if(mask >> z & ) continue;
if(!G[v][z]) continue;
int nxtmask = mask | ( << z);
if(SZ(road[nxtmask][u][z])) continue;
road[nxtmask][u][z] = road[mask][u][v];
road[nxtmask][u][z].push_back(z);
}
}
}
}
dp[] = ;
for(int mask = ; mask < ( << n); mask++) {
if(dp[mask] >= inf) continue;
for(int u = ; u < n; u++) {
if(mask >> u & ) continue;
if(!vtomask[u][mask]) continue;
if(vtomask[u][mask] >= ) {
int nxtmask = mask | ( << u);
if(chkmin(dp[nxtmask], dp[mask] + )) {
pre[nxtmask][] = mask;
pre[nxtmask][] = u;
pre[nxtmask][] = -;
}
}
for(int v = u + ; v < n; v++) {
if(mask >> v & ) continue;
if(!vtomask[v][mask]) continue;
int amask = (( << n) - ) ^ mask ^ ( << u) ^ ( << v);
for(int smask = amask; ; smask = (smask - ) & amask) {
int tmask = smask | ( << u) | ( << v);
if(SZ(road[tmask][u][v])) {
if(chkmin(dp[mask | tmask], dp[mask] + SZ(road[tmask][u][v]) + )) {
pre[mask | tmask][] = mask;
pre[mask | tmask][] = u;
pre[mask | tmask][] = v;
}
}
if(!smask) break;
}
}
}
}
printf("%d\n", dp[( << n) - ]);
int mask = ( << n) - ;
while(mask != ) {
int pmask = pre[mask][];
int u = pre[mask][];
int v = pre[mask][];
if(~v) {
int tmask = mask ^ pmask;
for(int i = ; i < SZ(road[tmask][u][v]); i++)
ans.push_back(mk(road[tmask][u][v][i - ], road[tmask][u][v][i]));
for(int i = ; i < n; i++) {
if((pmask >> i & ) && G[u][i]) {
ans.push_back(mk(i, u));
break;
}
}
for(int i = ; i < n; i++) {
if((pmask >> i & ) && G[v][i]) {
ans.push_back(mk(i, v));
break;
}
}
} else {
for(int i = , c = ; c; i++) {
if(G[u][i] && (pmask >> i & )) {
ans.push_back(mk(u, i));
c--;
}
}
}
mask = pmask;
}
for(auto& t : ans) printf("%d %d\n", t.fi + , t.se + );
return ;
} /*
*/
Codeforces 1155F Delivery Oligopoly dp(看题解)的更多相关文章
- Codeforces 513E2 Subarray Cuts dp (看题解)
我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...
- Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...
- Codeforces 1017F The Neutral Zone (看题解)
这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...
- Codeforces 865C Gotta Go Fast 二分 + 期望dp (看题解)
第一次看到这种骚东西, 期望还能二分的啊??? 因为存在重置的操作, 所以我们再dp的过程中有环存在. 为了消除环的影响, 我们二分dp[ 0 ][ 0 ]的值, 与通过dp得出的dp[ 0 ][ 0 ...
- Codeforces 442D Adam and Tree dp (看题解)
Adam and Tree 感觉非常巧妙的一题.. 如果对于一个已经建立完成的树, 那么我们可以用dp[ i ]表示染完 i 这棵子树, 并给从fa[ i ] -> i的条边也染色的最少颜色数. ...
- Codeforces 1101F Trucks and Cities dp (看题解)
Trucks and Cities 一个很显然的做法就是二分然后对于每个车贪心取check, 这肯定会TLE, 感觉会给人一种贪心去写的误导... 感觉有这个误导之后很难往dp那个方向靠.. dp[ ...
- Codeforces 596D Wilbur and Trees dp (看题解)
一直在考虑, 每一段的贡献, 没想到这个东西能直接dp..因为所有的h都是一样的. #include<bits/stdc++.h> #define LL long long #define ...
- Codeforces 983C Elevator dp (看题解)
Elevator 怎么今天写啥题都不会写啊, 我是傻了吗.. 把电梯里面四个人的目标点当作状态, 然后暴力转移. #include<bits/stdc++.h> #define LL lo ...
- Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...
随机推荐
- vue 中的通过搜索框进行数据过滤的过程
<template> <div> <input type="text" v-model="searchId" placeholde ...
- I2C(三) linux3.4(内核分析)
目录 I2C(三) linux3.4(内核分析) (一)总线流程 bus.probe match i2c_device_probe (二)client注册 方式(一)静态加载 方式(二)指定设备 方式 ...
- java容器-Map
Map:基本思想是映射表(维护键-值对),HashMap,TreeMap,LinkedHashMap,ConcurrentHashMap等都是基于Map接口实现的map容器,他们特性不同,表现在效率, ...
- Apache Hadoop 2.9.2 的归档案例剖析
Apache Hadoop 2.9.2 的归档案例剖析 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章说明你对NameNode 工作原理是有深入的理解啦!我们知道 ...
- 数据结构与算法之美学习笔记:B+树(第48讲)
一.解决问题的前提是定义清楚问题 通过对一些模糊需求进行假设,来限定要解决问题的范围 根据某个值查找数据,比如 select * from use where id=1234: 根据区间值来查询某些数 ...
- 实现IOC功能的简单Spring框架
需求分析 设计一个含有IOC的简单Spring,要求含有对象注册.对象管理以及暴露给外部的获取对象功能. 项目设计 对于注册的对象用一个类BeanInfo来描述其信息,包括对象标识.全类名以及属性名与 ...
- 永续公债(or统一公债)的麦考利久期(Macaulay Duration)的计算
- print number
# -*- coding: utf-8 -*-"""------------------------------------------------- File Name ...
- Swift PlayGround无限Running问题
这个问题我想肯定很多人都有遇到过,如果你正好面试iOS,用这个playground写算法的话遇到这种情况只能hehe了-- 我是这样解决的,一开始我build project的时候选得是iOS的pla ...
- mysql—增删改查语句总结
关于MySQL数据库——增删改查语句集锦 一.基本的sql语句 CRUD操作: create 创建(添加) read 读取 update 修改 delete 删除 .添加数据 ,'n001','201 ...