loj#10078. 新年好(最短路)
题目:
解析:
亲戚只有五个,可以把它们看成2,3,4,5,6号点,分别跑最短路,记录一下距离,然后DFS一下
这题非常玄学,我开了一个\(12*12\)的数组,没有离散化,竟然过了,开到\(5050*5050\)就RE,玄学
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
int n, m, num, cnt, ans = INF;
int head[N], dis[N], home[N], d[12][12];
bool vis[N], mark[N];
struct node {
int v, nx, w;
} e[N];
struct edge {
int id, dis;
bool operator <(const edge &oth) const {
return this -> dis > oth.dis;
}
};
inline void add(int u, int v, int w) {
e[++num] = (node) {v, head[u], w}, head[u] = num;
}
priority_queue<edge>q;
void dijkstra(int S) {
memset(dis, INF, sizeof dis);
memset(vis, 0, sizeof vis);
dis[S] = 0;
q.push((edge) {S, 0});
while (!q.empty()) {
edge d = q.top();
q.pop();
int u = d.id;
if (vis[u]) continue;
vis[u] = 1;
for (int i = head[u]; ~i; i = e[i].nx) {
int v = e[i].v;
if (dis[v] > dis[u] + e[i].w)
q.push((edge) {v, dis[v] = dis[u] + e[i].w});
}
}
}
void dfs(int u, int sum, int cnt) {
if (cnt == 5) {
ans = min(ans, sum);
return;
}
if (sum > ans) return;
for (int i = 2; i <= 6; ++i) {
if (!mark[i]) {
mark[i] = 1;
dfs(i, sum + d[u][i], cnt + 1);
mark[i] = 0;
}
}
}
int main() {
memset(head, -1, sizeof head);
cin >> n >> m;
for (int i = 1; i <= 5; ++i) cin >> home[i];
for (int i = 1, x, y, z; i <= m; ++i)
cin >> x >> y >> z, add(x, y, z), add(y, x, z);
dijkstra(1);
for (int i = 1; i <= 5; ++i)
d[1][i + 1] = d[i + 1][1] = dis[home[i]];
for (int i = 1; i <= 5; ++i) {
dijkstra(home[i]);
for (int j = i + 1; j <= 5; ++j)
d[i + 1][j + 1] = d[j + 1][i + 1] = dis[home[j]];
}
mark[1] = 1;
dfs(1, 0, 0);
cout << ans << endl;
}
loj#10078. 新年好(最短路)的更多相关文章
- CJOI 05新年好 (最短路+枚举)
CJOI 05新年好 (最短路+枚举) 重庆城里有n个车站,m条双向公路连接其中的某些车站.每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费 ...
- 20101010 exam
目录 2018 10.10 exam 解题报告 T1:LOJ #10078 新年好 题目描述(原题来自:CQOI 2005): 输入格式: 输出格式: 样例输入: 样例输出: 数据范围与提示: 思路: ...
- loj 1379(最短路变形)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27087 思路:题目的意思是求S->T的所有路径中花费总和小于 ...
- loj 1099(最短路)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25956 思路:dist[v][0]代表走到点v的最短路,dist[ ...
- LOJ#6354. 「CodePlus 2018 4 月赛」最短路[最短路优化建图]
题意 一个 \(n\) 个点的完全图,两点之间的边权为 \((i\ xor\ j)*C\) ,同时有 \(m\) 条额外单向路径,问从 \(S\) 到 \(T\) 的最短路. \(n\leq 10^5 ...
- LOJ#3087. 「GXOI / GZOI2019」旅行者(最短路)
题面 传送门 题解 以所有的感兴趣的城市为起点,我们正着和反着各跑一边多源最短路.记\(c_{0/1,i}\)分别表示正图/反图中离\(i\)最近的起点,那么对于每条边\((u,v,w)\),如果\( ...
- LOJ P3953 逛公园 NOIP dp 最短路 拓扑排序
https://www.luogu.org/problemnew/show/P3953 开o2过了不开o2re一个点...写法如题 顺便一提这道题在我校oj是a不了的因为我校土豆服务器速度奇慢1s时限 ...
- [BZOJ5109][LOJ #6252][P4061][CodePlus 2017 11月赛]大吉大利,今晚吃鸡!(最短路+拓扑排序+传递闭包+map+bitset(hash+压位))
5109: [CodePlus 2017]大吉大利,晚上吃鸡! Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 107 Solved: 57[Sub ...
- @loj - 6354@「CodePlus 2018 4 月赛」最短路
目录 @description@ @solution@ @accepted code@ @details@ @description@ 企鹅国中有 N 座城市,编号从 1 到 N . 对于任意的两座城 ...
随机推荐
- 在 Visual Studio 中安装 FxCop 分析器
本文转自 微软官网 : https://docs.microsoft.com/zh-cn/visualstudio/code-quality/install-fxcop-analyzers?view= ...
- SpringBoot——配置文件详解【五】
前言 SpringBoot的配置文件 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application.properties application.yml 配置文件 ...
- axios二次封装
import axios from "axios" //请求拦截器 axios.interceptors.request.use(function (config) { retur ...
- UID、PID、PPID是什么?
UID是用户ID,PID是进程ID,PPID是父进程ID. UID UID 用户身份证明(User Identification)的缩写.UID用户在注册后,系统会自动的给你一个UID的数值.意思就是 ...
- Polling 、Long Polling 和 WebSocket
最近在学习研究WebSocket,了解到Polling 和Long Polling,翻阅了一些博文,根据自己的理解,做个学习笔记 Polling (轮询): 这种方式就是客户端定时向服务器发送http ...
- Unittest 类方法
import unittest,time from selenium import webdriver class TestClass(unittest.TestCase): @classmethod ...
- java web开发入门汇总
servlet 用java语言开发动态资源网站的技术,在doGet方法中拼接显示html,在doPost方法中提交数据.类似于.net的ashx技术. servlet生成的class文件存放在tomc ...
- Centos开发小计
1. 生成静态库,linux下库的规则是lib开头 g++ -c code.cpp ar cr libcode.a code.o
- k8s删除pod一直处于terminating状态
我这里的pod是与nfs有关,nfs挂载有问题导致pod有问题,执行完删除命令以后看到pod一直处于terminating的状态. 这种情况下可以使用强制删除命令: kubectl delete po ...
- 【06月05日】A股滚动市净率PB历史新低排名
2010年01月01日 到 2019年06月05日 之间,滚动市净率历史新低排名. 上市三年以上的公司,2019年06月05日市净率在30以下的公司. 来源:A股滚动市净率(PB)历史新低排名. 1 ...