看别人写的才学会的。。。

我们考虑刚开始的一个点, 然后我们枚举接上去的一条一条链,

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(看题解)的更多相关文章

  1. Codeforces 513E2 Subarray Cuts dp (看题解)

    我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...

  2. Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)

    New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...

  3. Codeforces 1017F The Neutral Zone (看题解)

    这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...

  4. Codeforces 865C Gotta Go Fast 二分 + 期望dp (看题解)

    第一次看到这种骚东西, 期望还能二分的啊??? 因为存在重置的操作, 所以我们再dp的过程中有环存在. 为了消除环的影响, 我们二分dp[ 0 ][ 0 ]的值, 与通过dp得出的dp[ 0 ][ 0 ...

  5. Codeforces 442D Adam and Tree dp (看题解)

    Adam and Tree 感觉非常巧妙的一题.. 如果对于一个已经建立完成的树, 那么我们可以用dp[ i ]表示染完 i 这棵子树, 并给从fa[ i ] -> i的条边也染色的最少颜色数. ...

  6. Codeforces 1101F Trucks and Cities dp (看题解)

    Trucks and Cities 一个很显然的做法就是二分然后对于每个车贪心取check, 这肯定会TLE, 感觉会给人一种贪心去写的误导... 感觉有这个误导之后很难往dp那个方向靠.. dp[ ...

  7. Codeforces 596D Wilbur and Trees dp (看题解)

    一直在考虑, 每一段的贡献, 没想到这个东西能直接dp..因为所有的h都是一样的. #include<bits/stdc++.h> #define LL long long #define ...

  8. Codeforces 983C Elevator dp (看题解)

    Elevator 怎么今天写啥题都不会写啊, 我是傻了吗.. 把电梯里面四个人的目标点当作状态, 然后暴力转移. #include<bits/stdc++.h> #define LL lo ...

  9. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)

    Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...

随机推荐

  1. 原生js实现平滑滚动

    在以前的项目中有用到,在此整理一下: html部分 <span id="gotop">回到顶部</span> JS部分 // 使用requestAnimat ...

  2. 在php中实现Redis的订阅与发布

    <?php //require_once dirname(__FILE__).'/class/RedisClass.class.php'; function init_redis(){ $red ...

  3. Apache Shiro Java反序列化漏洞分析

    1. 前言 最近工作上刚好碰到了这个漏洞,当时的漏洞环境是: shiro-core 1.2.4 commons-beanutils 1.9.1 最终利用ysoserial的CommonsBeanuti ...

  4. HttpClient在多线程环境下踩坑总结

    问题现场 在多线程环境下使用HttpClient组件对某个HTTP服务发起请求,运行一段时间之后发现客户端主机CPU利用率呈现出下降趋势,而不是一个稳定的状态. 而且,从程序日志中判断有线程处于han ...

  5. Storage 001 电商数据库设计

    [大概流程 ]用户登录 > 选购商品 > 加入购物车 > 检查库存 >提交订单    >  选择在线支付  或 选择货到付款 > 发货 [用户模块]注册 登陆 [商 ...

  6. 理解Java的NIO

    同步与阻塞 同步和异步是针对应用程序和内核的交互而言的. 同步:执行一个操作之后,进程触发IO操作并等待(阻塞)或者轮询的去查看IO的操作(非阻塞)是否完成,等待结果,然后才继续执行后续的操作. 异步 ...

  7. iTOP-4418开发板Android 5.1/4.4丨Linux + Qt5.7丨Ubuntu12.04系统

    核心板参数 尺寸:50mm*60mm 高度:核心板连接器组合高度1.5mm PCB层数:6层PCB沉金设计 4418 CPU:ARM Cortex-A9 四核 S5P4418处理器 1.4GHz 68 ...

  8. Idea主题下载

    http://www.riaway.com/ 将jar导入

  9. my.ini的路径分隔符

    又踩了个坑,今天安装mysql,路径为F:\test\mysql于是我配置my.ini如下 [mysqld] basedir=F:\test\mysql datadir=F:\test\mysql\d ...

  10. Python 变量作用域,闭包和装饰器

    from dis import dis b = 6 def f1(a): print(a)print(b) b = 9 f1(3) print(dis(f1)) # dis模块可以查看python函数 ...