当前位置:网站首页>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;
}
边栏推荐
- Enjoy yuan mode -- a large number of flying dragons
- Jar package operation
- Self signed certificate generation
- The Google File System (GFS) learning notes
- Steps of JMeter performance test
- 云计算发展的 4 个阶段,终于有人讲明白了
- 主数据建设的背景
- Nifi fast authentication configuration
- Pyaudio audio recording
- Agency mode -- Jiangnan leather shoes factory
猜你喜欢

传统的IO存在什么问题?为什么引入零拷贝的?

Summary of message protocol problems

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

OSI notes sorting

Haitai Advanced Technology | application of privacy computing technology in medical data protection

After 5 months' test, it took 15K to come for an interview. When I asked, it was not worth even 5K. It was really

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

Interpreter mode -- formulas for dating

Network security review office starts network security review on HowNet

Read all text from stdin to a string
随机推荐
Sequential stack traversal binary tree
Background of master data construction
Basic properties and ergodicity of binary tree
Common self realization functions in C language development
Create a multithreaded thread class
Where is 5g really powerful? What is the difference with 4G?
Handling of garbled JMeter response data - three solutions
Power apps Guide
Static routing job
Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business
刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
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
Appium introduction and environment installation
Basic concepts and definitions of Graphs
Common member methods of the calendar class
Bean lifecycle flowchart
Leetcode (455) - distribute cookies
Pyaudio audio recording
JMeter basic learning records
Reflection - class object function - get method (case)