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

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

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. Java设计模式--装饰器模式到Java IO 流

    装饰器模式 抽象构件角色:给出一个抽象接口,以规范准备接受附加责任的对象. 具体构件角色:定义准备接受附加责任的对象. 抽象装饰角色:持有一个构件对象的实例,并对应一个与抽象构件接口一致的接口. 具体 ...

  2. JavaEEspring整理

    Spring框架—控制反转(IOC)            1 Spring框架概述                    1.1 什么是Spring                    1.2 S ...

  3. Unity 案例

    Unity 案例-用Unity 开发的产品 水电站管理.监控.培训系统 石油加工管理系统 房地产开发 污水处理系统 陆海空军事训练 城市和社区监控,管理系统 虚拟展馆 家庭自动化系统 石油加工管理系统 ...

  4. 【JS】前端文件下载(无刷新)方法总结

    #传统方法 利用iframe 或 form.submit 或 windows.open直接向后端发请求,后端返回文件流,后端处理成功后会直接返回到页面,浏览器会整理并打开自己的保存下载文件机制 . 1 ...

  5. Livereload or meta

    静态页面布局的过程中,如果可以一边写一边看见结果,那肯定是很方便的,在最开始使用的DW中实现了这一目标,但并不是浏览器环境下.之后使用gulp中的livereload后配合chrome插件livere ...

  6. 浏览器将URL变成一个屏幕上显示的网页的过程?

    前言 一个浏览器是怎么工作的? 正文 URL变网页过程: 1.浏览器通过http或https协议,向服务端请求页面 2.将请求过来的HEML代码通过解析,构建DOM树 3.计算DOM树上的CSS属性 ...

  7. grep sed awk 3个Linux中对文件内容操作的命令

    在学习Linux命令中,发现3个有关于文件内容操作的命令grep,sed和awk,在这里简单汇总这3个命令主要作用,在实际中找到最合适的情景应用,详细用法可以参考其他文章. 1.grep命令 主要作用 ...

  8. 一个疑问,int对象5为何没有__dict__属性,而类却有,这是怎么做到的?对象不是都可以调用类属性吗?

    a=1 print hasattr(a.__dict__) print hasattr(a.__class__.__dict__)

  9. 关于Android8及以上版本自定义广播无法接收问题

    高版本Android直接发送广播是无法接收的 e.g. sendBraodcast(intent); 需要在前面加上(前一个String为接收广播应用的项目地址,后一个为该广播类的地址) intent ...

  10. 三、vue之router

    三.vue之router 此时vue的脚手架.创建项目已经完成. ... vue的运行流程 index.html-->main.js-->App.vue-->router/index ...