当前位置:网站首页>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;
}
边栏推荐
- The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time
- Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
- JMeter response assertion
- opds sql组件能不能将流程参数通过上下文传给下一个组件
- Subnet partition operation
- 刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
- Does the developer want to change to software testing?
- Common member methods of the calendar class
- 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
- Format method and parse method of dateformat class
猜你喜欢

Basic concepts and definitions of Graphs

顺序表的基本操作

Learn to use a new technology quickly

DHCP operation

Interpreter mode -- formulas for dating

Sleep revolution - find the right length of rest

Nifi fast authentication configuration

Sequential stack traversal binary tree

虚拟化是什么意思?包含哪些技术?与私有云有什么区别?

Curl command
随机推荐
Where is 5g really powerful? What is the difference with 4G?
Popupwindow touch event transparent transmission scheme
Undo log and redo log must be clear this time
Reflect package
[performance tuning basics] performance tuning standards
等保备案是等保测评吗?两者是什么关系?
Procedural life: a few things you should know when entering the workplace
伯克利、MIT、劍橋、DeepMind等業內大佬線上講座:邁向安全可靠可控的AI
微信小程序中使用vant组件
2021-09-30
Difference between map and object
在Dialog中使用透明的【X】叉叉按钮图片
Steps of JMeter performance test
Adding subscribers to a list using mailchimp's API V3
What are the problems with traditional IO? Why is zero copy introduced?
A/B测试助力游戏业务增长
Jar package operation
Splicing audio files with ffmpeg-4.3
Format method and parse method of dateformat class
刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。