当前位置:网站首页>Dijkstra seeking secondary short circuit (easy to understand)
Dijkstra seeking secondary short circuit (easy to understand)
2022-06-24 21:09:00 【GS_ Qiang】
step
1. Find the shortest path from the starting point to each point and use dis1 Write down the array ;
2. Find the shortest path at each point and use dis2 Write down the array ;
3. Access each edge ( If it is an undirected graph, it needs to traverse the given number of edges *2), use u Indicates the starting point of this edge ,v Indicates the end of this side ,w Weight representation , use k Write down the starting point to u The shortest path plus v The shortest path to the destination plus w;
4. Compare k and dis1[ t ] Size , If k > dis1[ t ], Just like ans Compare , If it is less than ans, Update ans The value of is k;
5. A short circuit is ans;
Code :
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=100005;
int t;
ll n,m,k;
ll u[N],v[N],w[N];
ll Min;
bool vis[N];
ll a[N];
ll dis[N],dis1[N],dis2[N];
struct node {
ll to,dis;
node(ll a,ll b){
to=a;dis=b;}
friend bool operator <(node a,node b) {
return a.dis > b.dis;
}
};
struct Edge {
ll to,cost,next;
}E[N*5];
ll head[N*5],tot;
void add(ll from,ll to,ll v) {
E[tot].to=to;
E[tot].cost=v;
E[tot].next=head[from];
head[from]=tot++;
}
void init() {
memset(dis1,INF,sizeof(dis1));
memset(dis2,INF,sizeof(dis2));
memset(head,-1,sizeof(head));
tot=0;
}
priority_queue<struct node>que;
void dijkstra(ll s) {
memset(vis,false,sizeof(vis));
memset(dis,INF,sizeof(dis));
que.push(node(s,0));
dis[s]=0;
while(!que.empty()) {
node now=que.top();
que.pop();
if(vis[now.to]) continue;
vis[now.to]=true;
for(ll i=head[now.to];~i;i=E[i].next) {
ll to=E[i].to;
ll costlen=E[i].cost+dis[now.to];
if(dis[to] > costlen) {
dis[to]=costlen;
if(!vis[to])
que.push(node(to,costlen));
}
}
}
}
int main() {
while(~scanf("%lld %lld",&n,&m)) {
init();
for(ll i=1;i<=m;i++) {
scanf("%lld %lld %lld",&u[i],&v[i],&w[i]);
u[i+n]=v[i];
v[i+n]=u[i];
w[i+n]=w[i];
add(u[i],v[i],w[i]);
add(v[i],u[i],w[i]);
}
scanf("%lld",&k);
dijkstra(1);
for(ll i=1;i<=n;i++) dis1[i]=dis[i];
dijkstra(n);
for(ll i=1;i<=n;i++) dis2[i]=dis[i];
Min=INF;
for(ll i=1;i<=2*m;i++) {
ll x=u[i],y=v[i],z=w[i];
ll va=dis1[x]+dis2[y]+z;
if(va>dis1[n] && va<Min) Min=va;
}
printf("%d\n",k==1?Min:dis1[n]);
}
return 0;
}
边栏推荐
- [performance tuning basics] performance tuning strategy
- Leetcode(146)——LRU 缓存
- The four stages of cloud computing development have finally been clarified
- yeb_ Back first day
- Basic concepts and definitions of Graphs
- Analysis of errors in JSON conversion using objectmapper
- Power apps Guide
- 传统的IO存在什么问题?为什么引入零拷贝的?
- What will you do if you have been ignored by your leaders at work?
- Go coding specification
猜你喜欢
Image panr
浅谈MySql update会锁定哪些范围的数据
Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
[email protected] -Perfmon metric collector listener steps"/>
JMeter installation plug-in, adding [email protected] -Perfmon metric collector listener steps
Rip/ospf protocol notes sorting
I feel that I am bald again when I help my children with their homework. I feel pity for my parents all over the world
Summary of idea practical skills: how to rename a project or module to completely solve all the problems you encounter that do not work. It is suggested that the five-star collection be your daughter
Mapstacks: data normalization and layered color layer loading
网络安全审查办公室对知网启动网络安全审查
主数据建设的背景
随机推荐
Pytest testing framework
Splicing audio files with ffmpeg-4.3
C langage pour le déminage (version simplifiée)
Vant component used in wechat applet
How to enhance influence
maptalks:数据归一化处理与分层设色图层加载
The AI for emotion recognition was "harbouring evil intentions", and Microsoft decided to block it!
Pyaudio audio recording
After a few years in the testing industry, do you still know a little?
Leetcode(135)——分发糖果
Basic properties and ergodicity of binary tree
云计算发展的 4 个阶段,终于有人讲明白了
Reflect package
Variable setting in postman
Limit summary (under update)
NPM download speed is slow
全上链哈希游戏dapp系统定制(方案设计)
Shrimp skin test surface treated
Camera rental management system based on qt+mysql
Second understanding permutation and combination