当前位置:网站首页>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;
} 边栏推荐
- Notes | numpy-07 Slice and index
- 论文阅读_ICD编码_MSMN
- [tools run SQL blind note]
- I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
- sql语句模糊查询遇到的问题
- Review the old and know the new: Notes on Data Science
- 1119 pre- and post order traversals (30 points)
- Do you know UVs in modeling?
- Notes | numpy-10 Iterative array
- Learn to use the idea breakpoint debugging tool
猜你喜欢

Cross platform plug-in flutter for displaying local notifications_ local_ notifications

On typescript and grammar

论文阅读_ICD编码_MSMN

Web security - CSRF (token)

MC Layer Target

Thesis reading_ Tsinghua Ernie

Without 50W bride price, my girlfriend was forcibly dragged away. What should I do

并发操作-内存交互操作
![[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)](/img/34/d195752992f8955bc2a41b4ce751db.jpg)
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
![[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached](/img/01/052928e7f20ca671cdc4c30ae55258.jpg)
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
随机推荐
SSM framework integration
Blog building tool recommendation (text book delivery)
Do you know UVs in modeling?
逆袭大学生的职业规划
Keepalived热备与HAProxy
Number of 1 in binary (simple difficulty)
I stepped on a foundation pit today
Compile and decompile GCC common instructions
Learn to use the idea breakpoint debugging tool
2022-02-12 daily clock in: problem fine brush
Three representations of signed numbers: original code, inverse code and complement code
[luatos sensor] 2 air pressure bmp180
Truncated sentences of leetcode simple questions
Handler understands the record
关于开学的准备与专业认知
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Games101 Lesson 9 shading 3 Notes
Network security textual research recommendation
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement