当前位置:网站首页>1111 online map (30 points)
1111 online map (30 points)
2022-07-03 04:55:00 【vs5】
The main idea of the topic : shortest path ! Given n A little bit ,m side , It may be undirected , It may be a directed edge , Give the distance between two points , Time , Finally, give the starting point and ending point , Find the shortest path from the starting point to the end point and the fastest arrival path .
Shortest path : If the shortest path is not unique , The same shortest path takes the fastest path .
The fastest way to reach : If the fastest path to reach is not unique , Output the path through the least points .
If the path is the same, output Distance = D; Time = T: source -> u1 -> ... -> destination
If not equal, output
Distance = D: source -> v1 -> ... -> destination
Time = T: source -> w1 -> ... -> destination
Run the shortest way twice , But there is no need to write two dijkstra, Write a dijkstra Judge which one you want by passing parameters .
Plain version , And heap optimization .
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> PII;
const int N = 260010;
int e[N],ne[N],w[N],ti[N],h[N],dist[N],fast[N],path[N],idx;
bool st[N];
int n,m,s1,s2;
void add(int a,int b,int c,int d)
{
e[idx] = b,ne[idx] = h[a],w[idx] = c,ti[idx] = d,h[a] = idx ++;
}
string pa,pb;
void dfs(int u,string &p)// Take the path
{
if(path[u] == -1)
{
p += to_string(u);return ;
}
dfs(path[u],p);
p = p + " -> " + to_string(u) ;
}
void dijkstra(int w1[],int w2[],int flag)
{
memset(dist,0x3f,sizeof dist);
memset(fast,0x3f,sizeof fast);
memset(st,false,sizeof st);
dist[s1] = 0,fast[s1] = 0;
priority_queue<PII,vector<PII>,greater<PII>> que;
que.push({0,s1});
path[s1] = -1;
while(que.size())
{
auto t = que.top();que.pop();
int u = t.second;
if(st[u]) continue;
if(u == s2) break;
st[u] = true;
for(int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if(flag) w2[i] = 1;// Finding the shortest time requires set points
if(!st[j] && dist[j] > dist[u] + w1[i])
{
dist[j] = dist[u] + w1[i];//0. shortest path 1. The shortest time
fast[j] = fast[u] + w2[i];//0. The shortest time 1. Path minimum point
path[j] = u;
que.push({dist[j],j});
}
else if (dist[j] == dist[u] + w1[i] && fast[j] > fast[u] + w2[i])
{
fast[j] = fast[u] + w2[i];
path[j] = u;
}
}
}
}
int main()
{
cin.tie(0);cout.tie(0);
cin >> n >> m;
memset(h,-1,sizeof h);
while(m --)
{
int a,b,c,d,e;
cin >> a >> b >> c >> d >> e;
add(a,b,d,e);
if(!c) add(b,a,d,e);
}
cin >> s1 >> s2;
dijkstra(w,ti,0);int mindist = dist[s2];dfs(s2,pa);
dijkstra(ti,w,1);int mintime = dist[s2];dfs(s2,pb);
if(pa == pb) printf("Distance = %d; Time = %d: %s",mindist,mintime,pa.c_str());
else printf("Distance = %d: %s\nTime = %d: %s",mindist,pa.c_str(),mintime,pb.c_str());
return 0;
}边栏推荐
- [set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
- Silent authorization login and registration of wechat applet
- 关于开学的准备与专业认知
- Day 51 - tree problem
- Distinguish between releases and snapshots in nexus private library
- Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
- Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
- Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
- SSM framework integration
- Source insight garbled code solution
猜你喜欢
![[USACO 2009 Dec S]Music Notes](/img/e6/282a8820becdd24d63dcff1b81fcaf.jpg)
[USACO 2009 Dec S]Music Notes

Review the configuration of vscode to develop golang

Number of 1 in binary (simple difficulty)

C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement

The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection

移动端——uniapp开发记录(公共请求request封装)

2022-02-11 daily clock in: problem fine brush

Source insight garbled code solution

MC Layer Target

【XSS绕过-防护策略】理解防护策略,更好的绕过
随机推荐
[luatos sensor] 2 air pressure bmp180
Notes | numpy-07 Slice and index
Hire cashier (differential constraint)
Number of uniform strings of leetcode simple problem
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
Market status and development prospect prediction of global colorimetric cup cover industry in 2022
Notes | numpy-08 Advanced index
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
Thesis reading_ Tsinghua Ernie
【SQL注入点】注入点出现位置、判断
【工具跑SQL盲注】
Wechat applet distance and map
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
Market status and development prospect prediction of global fermented plant protein industry in 2022
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
Why does I start with =1? How does this code work?
Shell script Basics - basic grammar knowledge
The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
Truncated sentences of leetcode simple questions