当前位置:网站首页>[Luogu p8063] shortest paths (graph theory)
[Luogu p8063] shortest paths (graph theory)
2022-07-26 16:55:00 【SSL_ TJH】
Shortest paths
Topic link :luogu P8063
The main idea of the topic
Here's an undirected graph , Then give you the shortest path , Then for each edge on the shortest path, ask what is the shortest path of the graph after you delete it , If there is no path, output -1.
Ideas
( Look at the question next to me , I can't make myself numb )
( It depends on our wyc A great god )
Consider how to do , That emotional thinker is the most short-circuit is gone. Find a slightly less bad one to sew .
Then how to sew , First of all, how do we go , For each point, it is to walk the shortest path tree to get .
Then the two sides are sewn up, so you have to run from the beginning to the end .
Then you consider deleting an edge , Two trees will drop a subtree respectively .
Then you think about the rest , There are two situations :
One is that they intersect , It can be directly connected by these points .
The other is to connect by the side ( Then note that it can't be the edge you deleted ), Then you can see whether you can be in one of the two sets .
Then you can O ( n 2 + n m ) O(n^2+nm) O(n2+nm) Ask for the answer .
Code
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int N = 2050;
const int M = 1e5 + 100;
struct node {
int x, y, z;
}a[M];
int n, m, S, T, w[N][N], id[N][N], wow[N];
int dis[N], sid[N], faS[N], faT[N], na, nb;
vector <int> g[N], va[N], Gs[N], Gt[N];
bool in[N], goS[N], goT[N];
priority_queue <pair<int, int> > q;
void Dij(int S, int *f, int *fa) {
memset(in, 0, sizeof(in));
while (!q.empty()) q.pop();
q.push(make_pair(-0, S)); f[S] = 0;
while (!q.empty()) {
int now = q.top().second; q.pop();
if (in[now]) continue; in[now] = 1;
for (int i = 0; i < g[now].size(); i++) {
int x = g[now][i], val = va[now][i];
if (f[x] > f[now] + val) {
f[x] = f[now] + val; q.push(make_pair(-f[x], x)); fa[x] = now;
}
}
}
}
void Init() {
memset(dis, 0x7f, sizeof(dis)); memset(sid, 0x7f, sizeof(sid));
Dij(S, dis, faS); Dij(T, sid, faT);
for (int i = 1; i <= n; i++) if (i != S) Gs[faS[i]].push_back(i);
for (int i = 1; i <= n; i++) if (i != T) Gt[faT[i]].push_back(i);
}
bool check(int x, int y) {
if (x == na && y == nb) return 0;
if (x == nb && y == na) return 0;
return 1;
}
int clac() {
memset(goS, 0, sizeof(goS)); memset(goT, 0, sizeof(goT));
queue <int> q;
q.push(S);
while (!q.empty()) {
int now = q.front(); q.pop();
goS[now] = 1;
for (int i = 0; i < Gs[now].size(); i++) {
int x = Gs[now][i];
if (check(now, x)) q.push(x);
}
}
q.push(T);
while (!q.empty()) {
int now = q.front(); q.pop();
goT[now] = 1;
for (int i = 0; i < Gt[now].size(); i++) {
int x = Gt[now][i];
if (check(now, x)) q.push(x);
}
}
int ans = 2e9;
for (int i = 1; i <= n; i++) if (goS[i] && goT[i]) ans = min(ans, dis[i] + sid[i]);
for (int i = 1; i <= m; i++) {
if (!check(a[i].x, a[i].y)) continue;
if (goS[a[i].x] && goT[a[i].y]) ans = min(ans, dis[a[i].x] + a[i].z + sid[a[i].y]);
if (goS[a[i].y] && goT[a[i].x]) ans = min(ans, dis[a[i].y] + a[i].z + sid[a[i].x]);
}
return (ans == 2e9) ? -1 : ans;
}
int main() {
scanf("%d %d %d %d", &n, &m, &S, &T);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].z);
w[a[i].x][a[i].y] = w[a[i].y][a[i].x] = a[i].z;
id[a[i].x][a[i].y] = id[a[i].y][a[i].x] = i;
g[a[i].x].push_back(a[i].y); g[a[i].y].push_back(a[i].x);
va[a[i].x].push_back(a[i].z); va[a[i].y].push_back(a[i].z);
}
scanf("%d", &wow[0]); for (int i = 1; i <= wow[0]; i++) scanf("%d", &wow[i]);
Init();
for (int i = 2; i <= wow[0]; i++) {
na = wow[i - 1]; nb = wow[i];
printf("%d\n", clac());
}
return 0;
}
边栏推荐
- C#转整型的三种方式的区别以及效率对比
- JS API summary of Array Operations
- About the idea plug-in I wrote that can generate service and mapper with one click (with source code)
- 快速学会配置yum的本地源和网络源,并学会yum的使用
- The difference and efficiency comparison of three methods of C # conversion integer
- 正则表达式
- Oracle创建表分区后,查询的时候不给出partition,但是会给分区字段指定的值,会不会自动按照分区查询?
- 6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
- ES:Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
- TCP 和 UDP 可以使用相同端口吗?
猜你喜欢

37.【重载运算符的类别】

如何保证缓存和数据库一致性

Operating system migration practice: deploying MySQL database on openeuler

How to write unit tests

2022软件测试技能 Postman+newman+jenkins 持续集成 实战教程

【Express接收Get、Post、路由请求参数】

导数、微分、偏导数、全微分、方向导数、梯度的定义与关系

Nacos win10 installation and configuration tutorial

面试时候常说的复杂度到底是什么?

Wechat applet - network data request
随机推荐
How to balance open utilization and privacy security compliance of public data?
docker安装redis?如何配置持久化策略?
Comprehensively design an oppe homepage -- layout and initialization
Final consistency distributed transaction TCC
Alibaba Cloud Toolkit —— 项目一键部署工具
我的sql没问题为什么还是这么慢|MySQL加锁规则
Re7: reading papers fla/mlac learning to predict charges for critical cases with legal basis
Docker install redis? How to configure persistence policy?
匿名方法和lambda表达式使用的区别
营销指南 | 几种常见的微博营销打法
Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
A preliminary understanding of MVC and ECS design architectures
Guetzli simple to use
Win11怎么重新安装系统?
Stop using xshell and try this more modern terminal connection tool
Understanding JS foundation and browser engine
ES:Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes
Marketing guide | several common micro blog marketing methods
How to implement Devops with automation tools | including low code and Devops application practice
【飞控开发基础教程1】疯壳·开源编队无人机-GPIO(LED 航情灯、信号灯控制)