题意及思路:https://blog.csdn.net/yzyyylx/article/details/90145400

这题主要巧妙在分析了最短路算法的性质,得出大小小于等于3的连通块一定不会被再次访问的结论。

代码:

#include <bits/stdc++.h>
using namespace std;
bool v[80][1 << 18];
int dp[80][1 << 18];
const int maxn = 1010;
struct node {
int dis, state, now;
bool operator < (const node& rhs) const {
return dis > rhs.dis;
}
};
struct Edge {
int v, w;
};
vector<Edge> G[maxn];
int sz[maxn], num[maxn], ans[maxn];
int A, B, n, m, tot;
void add(int x, int y, int z) {
G[x].push_back((Edge){y, z});
G[y].push_back((Edge){x, z});
}
priority_queue<node> q;
int f[maxn];
int get(int x) {
if(x == f[x]) return x;
return f[x] = get(f[x]);
}
void merge(int x, int y) {
int x1 = get(x), y1 = get(y);
if(x1 == y1) return;
f[x1] = y1;
sz[y1] += sz[x1];
}
int get_num(int x) {
if(sz[get(x)] > 3)
return 1 << num[get(x)];
else
return 0;
}
void dijkstra() {
memset(dp, 0x3f, sizeof(dp));
memset(ans, 0x3f, sizeof(ans));
q.push((node){0, get_num(1), 1});
dp[1][get_num(1)] = 0;
while(!q.empty()) {
node tmp = q.top();
q.pop();
if(v[tmp.now][tmp.state]) continue;
v[tmp.now][tmp.state] = 1;
ans[tmp.now] = min(ans[tmp.now], tmp.dis);
for (auto x : G[tmp.now]) {
if(x.w == B && get(x.v) == get(tmp.now)) continue;
if(x.w == B && (tmp.state & get_num(x.v))!= 0) continue;
if(x.w + tmp.dis < dp[x.v][get_num(x.v) | tmp.state]) {
dp[x.v][get_num(x.v) | tmp.state] = x.w + tmp.dis;
q.push((node){dp[x.v][get_num(x.v) | tmp.state], get_num(x.v) | tmp.state, x.v});
}
}
}
}
int main() {
int x, y, z;
scanf("%d%d%d%d", &n, &m, &A, &B);
for (int i = 1; i <= n; i++) {
f[i] = i;
sz[i] = 1;
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
if(z == A) {
merge(x, y);
}
}
for (int i = 1; i <= n; i++) {
if(i == get(i) && sz[i] > 3) {
num[i] = tot++;
}
}
dijkstra();
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
}

  

Codeforces 1149D 最短路 状压DP的更多相关文章

  1. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  2. HDU 4568 Hunter 最短路+状压DP

    题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...

  3. codeforces 21D. Traveling Graph 状压dp

    题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...

  4. 最短路+状压DP【洛谷P3489】 [POI2009]WIE-Hexer

    P3489 [POI2009]WIE-Hexer 大陆上有n个村庄,m条双向道路,p种怪物,k个铁匠,每个铁匠会居住在一个村庄里,你到了那个村庄后可以让他给你打造剑,每个铁匠打造的剑都可以对付一些特定 ...

  5. 【BZOJ1097】[POI2007]旅游景点atr 最短路+状压DP

    [BZOJ1097][POI2007]旅游景点atr Description FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺 ...

  6. HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP

    题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others)  ...

  7. hdu3247Resource Archiver (AC自动机+最短路+状压dp)

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submis ...

  8. Codeforces 917C - Pollywog(状压 dp+矩阵优化)

    UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...

  9. Codeforces 79D - Password(状压 dp+差分转化)

    Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...

随机推荐

  1. 进程启动到别的session下(作用)

    https://blog.csdn.net/lostwifi/article/details/76472868 WTSGetActiveConsoleSessionId WTSEnumerateSes ...

  2. 42th 逻辑的连续性 取决于 细节的理解-------------我是个厨子:类的调用vs 对象调用方法

    类的调用vs 对象调用方法   class Cook5:    '''这是一个厨师的类'''        # 类是一系列对象相同的特征与技能的结合体        # 用变量表示特征(属性)    ...

  3. 【Java学习笔记】2015.1.5

    1.classpath中的“.”表示当前的路径,所以配置classpath时要注意不能忘了“.”.如果不注意,会出现cmd下运行程序时,.java文件可以编译但是不能运行: 一般的classpath配 ...

  4. 几种RAID级别的比较

    等级 概要 冗余 盘数 读快 写快 RAID 0 便宜.快速.危险 No N Yes Yes RAID 1 高速度.简单.安全 Yes 2(通常) Yes No RAID 5 安全(速度)成本折中 Y ...

  5. Vue路由组件vue-router

    一.路由介绍 Creating a Single-page Application with Vue + Vue Router is dead simple. With Vue.js, we are ...

  6. java基础学习笔记四(异常)

    Java中的异常 Exception 如图可以看出所有的异常跟错误都继承与Throwable类,也就是说所有的异常都是一个对象. 从大体来分异常为两块: 1.error---错误 : 是指程序无法处理 ...

  7. 【leetcode】1003. Check If Word Is Valid After Substitutions

    题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...

  8. mysql的安裝

    记得上学的时候,“研究”过一次mysql,找了篇文章,在课堂上念了.至今已经10余年,居然没再碰过数据库,自以为做嵌入式不用数据库,回头一看,却已经out许久... 上网下到最新的mysql5.5,从 ...

  9. VIM的一些使用积累

    替换: :s/cst/dst/gc 黏贴后格式不对齐: gg=G 全选并黏贴 gg :"+yG

  10. chroot()使用

    好多的程序,都有使用chroot来是程序chroot到一个目录下面,来保护文件系统,今天在看snort代码的时候,看到了实现,就贴出一个测试程序来,实际上是比较简单的.    chroot()在lin ...