当前位置:网站首页>1087 all roads lead to Rome (30 points)
1087 all roads lead to Rome (30 points)
2022-07-03 04:55:00 【vs5】
The shortest path with multiple conditions , The data range is very small , Write a plain version directly .
With two map Save name corresponding number , Name corresponding to number
See the code
#include <iostream>
#include <unordered_map>
#include <cstring>
using namespace std;
const int N = 300;
int g[N][N],dis[N],disha[N],ha[N],path[N],cnt[N],res[N],n,m;
bool st[N];
string start,en = "ROM";
unordered_map<string,int>mp;
unordered_map<int,string>idx;
void dijkstra()
{
memset(dis,0x3f,sizeof dis);
dis[mp[start]] = 0;
res[mp[start]] = 1;
for(int i = 0; i < n; i ++)
{
int u = -1;
for(int j = 1; j <= n; j ++)
{
if(!st[j] && (u == -1 || dis[u] > dis[j])) u = j;
}
st[u] = true;
for(int j = 1; j <= n; j ++)
{
if(!st[j] && dis[j] > dis[u] + g[u][j])
{
dis[j] = dis[u] + g[u][j];// shortest path
disha[j] =disha[u] + ha[j];// Happiness
cnt[j] = cnt[u] + 1;// Path points
res[j] = res[u];// Number of shortest circuits
path[j] = u;// route
}
else if(dis[j] == dis[u] + g[u][j])
{
res[j] += res[u];
if(disha[j] < disha[u] + ha[j])
{
disha[j] = disha[u] + ha[j];
cnt[j] = cnt[u] + 1;
path[j] = u;
}
else if(disha[j] == disha[u] + ha[j])
if(cnt[j] > cnt[u] + 1)
{
cnt[j] = cnt[u] + 1;
path[j] = u;
}
}
}
}
}
void dfs(int u)
{
if(u == mp[start])
{
cout << idx[u];return ;
}
dfs(path[u]);
cout << "->" << idx[u];
}
int main()
{
cin >> n >> m >> start;
memset(g,0x3f,sizeof g);
mp[start] = 1;idx[1] = start;
for(int i = 2; i <= n ; i ++)
{
string s;int x;
cin >> s >> x;
mp[s] = i;idx[i] = s;ha[i] = x;
}
for(int i = 0; i < m; i ++)
{
string a,b;int x;
cin >> a >> b >> x;
g[mp[a]][mp[b]] = g[mp[b]][mp[a]] = x;
}
dijkstra();
cout << res[mp[en]] << ' ' << dis[mp[en]] << ' ' << disha[mp[en]] << ' ' << disha[mp[en]] / (cnt[mp[en]]) << endl;
dfs(mp[en]);
return 0;
}
边栏推荐
- 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
- [luatos sensor] 2 air pressure bmp180
- 1110 complete binary tree (25 points)
- [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)
- C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
- Market status and development prospect prediction of global neutral silicone sealant industry in 2022
- Current market situation and development prospect forecast of the global fire boots industry in 2022
- The process of browser accessing the website
- [luatos sensor] 1 light sensing bh1750
- On typescript and grammar
猜你喜欢
[luatos sensor] 2 air pressure bmp180
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
RT thread flow notes I startup, schedule, thread
Analysis of proxy usage of ES6 new feature
Truncated sentences of leetcode simple questions
Thesis reading_ Chinese medical model_ eHealth
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Triangular rasterization
The least operation of leetcode simple problem makes the array increment
[XSS bypass - protection strategy] understand the protection strategy and better bypass
随机推荐
Number of 1 in binary (simple difficulty)
1110 complete binary tree (25 points)
document. The problem of missing parameters of referer is solved
论文阅读_中文NLP_ELECTRA
Actual combat 8051 drives 8-bit nixie tube
I stepped on a foundation pit today
Kept hot standby and haproxy
Compile and decompile GCC common instructions
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
[USACO 2009 Dec S]Music Notes
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
Market status and development prospect forecast of global button dropper industry in 2022
Preparation for school and professional cognition
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
2022-02-11 daily clock in: problem fine brush
编译GCC遇到的“pthread.h” not found问题
Oracle SQL table data loss
Market status and development prospect prediction of the global fire extinguisher industry in 2022
Market status and development prospect prediction of the global fire hose industry in 2022
112 stucked keyboard (20 points)