当前位置:网站首页>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;
}
边栏推荐
- 伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI
- Variable setting in postman
- Reflection - class object function - get method (case)
- Time standard and format
- C langage pour le déminage (version simplifiée)
- C语言实现扫雷(简易版)
- How to enhance influence
- Wechat applet custom tabbar
- Geek University cloud native training camp
- Open function
猜你喜欢

网络安全审查办公室对知网启动网络安全审查

Enjoy yuan mode -- a large number of flying dragons

How Fiddler works

Does the developer want to change to software testing?

伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI

Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable

Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!

Wechat applet custom tabbar

Limit summary (under update)

云计算发展的 4 个阶段,终于有人讲明白了
随机推荐
How to enhance influence
More than ten years' work experience is recommended at the bottom of the box: how much does it cost to find a job? See here! Brothers and sisters are recommended to collect and pay attention
2021-09-30
Basic properties and ergodicity of binary tree
Wechat applet custom tabbar
Enjoy yuan mode -- a large number of flying dragons
Learn together and make progress together. Welcome to exchange
Batch capitalization of MySQL table names
yeb_ Back first day
Builder mode -- Master asked me to refine pills
Grating diffraction
Second understanding permutation and combination
Limit summary (under update)
Self signed certificate generation
伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
Appium introduction and environment installation
Variable setting in postman
科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
使用gorm查询数据库时reflect: reflect.flag.mustBeAssignable using unaddressable value
[普通物理] 光栅衍射