当前位置:网站首页>重庆OI 2005 新年好
重庆OI 2005 新年好
2022-07-29 23:17:00 【Vegetable newbie】
重庆OI 2005 新年好
题意
有 n n n个车站, m m m条双向边,给你指定的 5 5 5个车站 a , b , c , d , e a, b, c, d, e a,b,c,d,e,问你从 1 1 1号车站出发,经过所有指定车站的路径之和最短为多少。
数据范围
1 ⩽ n ⩽ 50000 1 \leqslant n \leqslant 50000 1⩽n⩽50000
1 ⩽ m ⩽ 1 0 5 1 \leqslant m \leqslant 10^5 1⩽m⩽105
1 < a , b , c , d , e ⩽ n 1 < a, b, c, d, e \leqslant n 1<a,b,c,d,e⩽n
思路
暴力枚举所有的行走路线一共 5 ! 5! 5!种方案,我们可以先预处理出这 6 6 6个车站(包括第一个车站)之间的最短距离,然后暴力跑所有的行走路线,取路径之和的最小值。
时间复杂度
用 d i j k s t r a dijkstra dijkstra跑,
O ( m ∗ log n ∗ 5 ! ) O(m * \log{n} * 5!) O(m∗logn∗5!)
代码
#include <bits/stdc++.h>
#define PII pair<int,int>
#define LL long long
#define fi first
#define se second
#define debug(a) cout<<#a<<"="<<a<<endl;
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define Yes puts("Yes");
#define No puts("No");
#define sz(x) (int)x.size()
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1; ch=getchar(); }
do {
(t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
template <typename T> void write(T t) {
if (t<0) {
putchar('-'); write(-t); return; }
if (t>9) write(t/10);
putchar('0'+t%10);
}
template <typename T> void writeln(T t) {
write(t); puts(""); }
const int N = 50010, M = 200010;
int n, m, sum, ans = 2e9;
int a[10];//亲戚家
int h[N], ne[M], e[M], w[M], idx;
int dist[10][N];
bool st[N], vis[N];
void add(int a, int b, int c){
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}
void dijkstra(int x, int d[]){
memset(st, false, sizeof st);
d[x] = 0;
priority_queue<PII,vector<PII>,greater<PII>>q;
q.push({
0, x});
while(q.size()){
auto it = q.top();
q.pop();
int ver = it.second;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; ~i; i = ne[i]) {
int j = e[i];
if(d[j] > d[ver] + w[i]){
d[j] = d[ver] + w[i];
q.push({
d[j], j});
}
}
}
}
void dfs(int pre, int cnt){
if(cnt == 6){
ans = min(ans, sum);
return;
}
for(int i = 2; i <= 6; i ++ ){
if(!vis[i]){
sum += dist[i][pre];
vis[i] = true;
dfs(a[i], cnt + 1);
vis[i] = false;
sum -= dist[i][pre];
}
}
}
int main()
{
memset(h, -1, sizeof h);
memset(dist, 127, sizeof dist);
read(n);read(m);
for(int i = 2; i <= 6; i ++ ) read(a[i]);
a[1] = 1;
while(m -- ){
int a, b, c;
read(a), read(b), read(c);
add(a, b, c);add(b, a, c);
}
for(int i = 1; i <= 6; i ++ ) dijkstra(a[i], dist[i]);
dfs(1, 1);
writeln(ans);
return 0;
}
边栏推荐
- Three chess (written in C language)
- devops学习(四) Jenkins CI 持续集成
- Another new rule for credit cards is coming!Juphoon uses technology to boost the financial industry to improve service quality and efficiency
- 一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
- DNA脱氧核糖核酸修饰石墨粉末|DNA修饰还原石墨烯功能材料|保存温度
- devops学习(三) K8环境部署jenkins
- cached_network_image 多个图片卡顿崩溃
- 真offer收割机 第一弹~大厂如何考察候选人?(附答案详解)
- 新版微信小程序发布指南
- 高数下|三重积分习题课|高数叔|手写笔记
猜你喜欢

真offer收割机 第一弹~大厂如何考察候选人?(附答案详解)

In 2022, the latest Gansu construction staff (material staff) mock exam questions and answers

设计消息队列存储消息的MySQL表格

DNA脱氧核糖核酸修饰四氧化三铁|DNA修饰氧化锌|使用方法

一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
![[leetcode] 82. Delete duplicate elements in sorted linked list II (medium)](/img/93/a744cfc059245de2fc07894167f3c5.png)
[leetcode] 82. Delete duplicate elements in sorted linked list II (medium)

Raspberry pie wiringPi 2.6 installed on solving gpio readall command mistakes

MySQL面试题:用户金额充值面试题详解

Install PyCharm on Raspberry Pi

玻璃表面修饰DNA|DNA修饰的上转换纳米材料|DNA-UCNPs实验原理
随机推荐
SAP ABAP 守护进程的实现方式
devops学习(四) Jenkins CI 持续集成
运动步数抽奖小程序开发
[2023 School Recruitment Questions] Summary of Common Interview Questions (7. Common Bus Protocols) (Continuously updated with subsequent interviews....)
devops学习(十) Jenkins 流水线
Gao Shu Xia|Triple Integral Exercises|Uncle Gao Shu|Handwritten Notes
devops学习(五) Jenkins 简单完成持续部署
esp12f + tft display picture problem
什么是色选机(color sorter)?
jenkins use and maintenance
y81. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Monitoring Extension (12)
浅析即时通讯移动端开发DNS域名劫持等杂症
[2023 School Recruitment Questions] Summary of knowledge points and hand-tear code in the written test and interview
Elementary C language - first understanding of C language
高数下|三重积分习题课|高数叔|手写笔记
MQTT over QUIC: The Next-Generation IoT Standard Protocol Brings New Impetus to Messaging Scenarios
子无序测试
devops学习(三) K8环境部署jenkins
Access Modbus TCP and Modbus RTU protocol devices using Neuron
地狱挖掘者系列#1