当前位置:网站首页>Luogu: p2685 [tjoi2012] Bridge
Luogu: p2685 [tjoi2012] Bridge
2022-07-03 17:09:00 【Elucidation】
Bridge
The question :
Boss Will guard a road , Make players from 1 Go to the n The shortest distance is as large as possible ;
Ideas :
obviously Boss Must guard 1 To n The shortest way , Otherwise, players can definitely take the shortest path .
So this problem becomes : Delete any edge on the shortest path , bring 1 To n The shortest distance is the largest
violence : We can certainly enumerate and delete every edge on the shortest path , Then run the shortest path again , But don't think the time complexity will explode ;
Then can we preprocess these situations , So you don't need to run the shortest circuit every time ?
The answer is yes :
- First of all 1、n Run for the starting point once each, the shortest way , In this way, we can get the graph formed by the interweaving of two shortest path trees ;
- Then we will start from the roots of these two shortest trees ( One left one right ) Run each time bfs, Record every point on the way from 1 ~ n Which point on the shortest path came from
Red is our shortest path , Green represents which point on the red side the point comes from ; With 1、n Run around for the root bfs, Every point has about l[i]、r[i] The mark of
- Since then, our pretreatment has been completed , You can find , Now we enumerate every edge that is not on the shortest path {a、b、w} , route {1 ~ n} It can be used {1 ~ l[a] ~ a ~ b ~ r[b] ~ n} Instead of
- What is the premise of this substitution ? Is the path {l[a] ~ r[b]} Any edge on the is occupied , When this path is occupied , We can use it dist1[a] + distn[b] + w Try to replace dist1[n]
- It can be found that this is an interval problem ,1 ~ n All the edges on the path are regarded as intervals , The information of interval maintenance is : When an edge in this interval is deleted, the 1 ~ n The shortest circuit value
- We can use segment tree to maintain this interval , Enumerate every side except the shortest path , For this side {a、b、w} , Modify on the segment tree {l[a],r[b]} Value
- Finally, traverse our segment tree , Take one of the information of each side in the end max That's our answer
See the notes for details
Code:
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#define mem(a,b) memset(a,b,sizeof a)
#define cinios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define sca scanf
#define pri printf
#define ul (u << 1)
#define ur (u << 1 | 1)
#define forr(a,b,c) for(int a=b;a<=c;a++)
#define rfor(a,b,c) for(int a=b;a>=c;a--)
//#define x first
//#define y second
//[ Blog address ](https://blog.csdn.net/weixin_51797626?t=1)
using namespace std;
inline int read() {
int x = 0, f = 1, ch = getchar(); while (ch < '0' || ch>'9') {
if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0'; ch = getchar(); } return x * f; }
inline void write(int x) {
if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N = 100010, M = 1000010, MM = N;
int INF = 0x3f3f3f3f, mod = 998244353;
ll LNF = 0x3f3f3f3f3f3f3f3f;
int n, m, k, T, S, D;
int h[N], e[M], ne[M], w[M], idx;
int dist1[N], distn[N], l[N], r[N];
bool st[N], isdis[M];
vector<int> arr;
int id[N], mx = -1, cnt;
inline void add(int a, int b, int x) {
e[idx] = b, ne[idx] = h[a], w[idx] = x, h[a] = idx++;
}
void dj(int* d, int x) {
mem(st, 0);
d[x] = 0;
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({
0,x });
while (q.size())
{
int ver = q.top().second;
q.pop();
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 bfs(int* l, int* d) {
queue<int> q;
for (auto j : arr) {
l[j] = j;
q.push(j);
}
while (q.size())
{
int t = q.front();
q.pop();
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (d[t] + w[i] == d[j] && !l[j]) {
// Maintain on the shortest path tree l[i]、r[i]
l[j] = l[t];
q.push(j);
}
}
}
}
struct tree
{
int l, r, w;
}tr[N << 2]; // Line segment tree board
void build(int u, int l, int r) {
tr[u] = {
l,r,INF };
if (l == r)return;
int mid = l + r >> 1;
build(ul, l, mid), build(ur, mid + 1, r);
}
inline void pushdown(int u) {
tr[ul].w = min(tr[ul].w, tr[u].w);
tr[ur].w = min(tr[ur].w, tr[u].w);
}
void modify(int u, int l, int r, int d) {
if (tr[u].l >= l && tr[u].r <= r) {
tr[u].w = min(tr[u].w, d);// Not normally required pushdown
return;
}
int mid = tr[u].l + tr[u].r >> 1;
if (mid >= l)modify(ul, l, r, d);
if (mid < r)modify(ur, l, r, d);
}
void query(int u) {
if (tr[u].l == tr[u].r) {
if (mx < tr[u].w && tr[u].w != INF) {
mx = tr[u].w;
cnt = 1;
}
else if (mx == tr[u].w)cnt++;// Cumulative number of schemes
return;
}
pushdown(u);// At the end of the query pushdown that will do
query(ul); query(ur);
}
int main() {
cinios;
cin >> n >> m;
mem(h, -1);
forr(i, 1, m) {
int a, b, x;
cin >> a >> b >> x;
add(a, b, x), add(b, a, x);
}
mem(dist1, 0x3f);
mem(distn, 0x3f);
dj(distn, n);
dj(dist1, 1);// Two shortest paths
int z = 1;
arr.push_back(z);// Record path
while (z != n) {
for (int k = h[z]; ~k; k = ne[k]) {
int j = e[k];
if (distn[z] == distn[j] + w[k]) {
// Remember to run from leaves to roots , Otherwise, there will be mistakes if there are forks
z = j;
arr.push_back(z);
isdis[k] = isdis[k ^ 1] = true;// The marking point will be wrong , To mark edges
break;
}
}
}
for (int j = 0; j < arr.size(); j++)
id[arr[j]] = j + 1;// Discrete to points on the line segment tree
bfs(l, dist1);
bfs(r, distn);// Handle each point l[i]、r[i]
build(1, 1, arr.size() - 1);// Build up trees , Is interval , Pay attention to the scope
for (int i = 0; i < idx; i++) {
if (isdis[i])continue;// Enumerate all edges that are not on the shortest path
int b = e[i], a = e[i ^ 1];
if (dist1[a] < dist1[b]) {
// Meet the context
modify(1, id[l[a]], id[r[b]] - 1, dist1[a] + distn[b] + w[i]);
}
}
query(1);
if (mx == dist1[n])cnt = m;// If the modification cannot reduce the shortest circuit length , Obviously, you can delete any side
cout << mx << ' ' << cnt;
return 0;
}
/* */
Marika ( A simplified version of this question , It doesn't seem to simplify much )
边栏推荐
- Redis:关于列表List类型数据的操作命令
- Kotlin learning quick start (7) -- wonderful use of expansion
- Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard
- MySQL user management
- Great changes! National housing prices fell below the 10000 yuan mark
- How SVN views modified file records
- Life is still confused? Maybe these subscription numbers have the answers you need!
- ANOVA example
- 13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
- 大消费企业怎样做数字化转型?
猜你喜欢
What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
[JDBC] API parsing
Great changes! National housing prices fell below the 10000 yuan mark
kubernetes资源对象介绍及常用命令(三)
Design e-commerce spike
Simple use of unity pen XR grab
C语言按行修改文件
Why is WPA3 security of enterprise business so important?
线程池:业务代码最常用也最容易犯错的组件
跨境电商:外贸企业做海外社媒营销的优势
随机推荐
[mathematical logic] equivalent calculus and reasoning calculus of propositional logic (propositional logic | equivalent calculus | principal conjunctive (disjunctive) paradigm | reasoning calculus)**
C language modifies files by line
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
kubernetes资源对象介绍及常用命令(三)
Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
One brush 144 force deduction hot question-1 sum of two numbers (E)
手把手带你入门 API 开发
数据分析必备的能力
CC2530 common registers for timer 1
Open vsftpd port under iptables firewall
基于主机的入侵系统IDS
How to judge the region of an IP through C?
vs code 插件 koroFileHeader
BYD and great wall hybrid market "get together" again
Mysql database -dql
网络硬盘NFS的安装与配置
[combinatorial mathematics] counting model, common combinatorial numbers and combinatorial identities**
Execute script unrecognized \r
Solution to long waiting time of SSH connection to remote host