当前位置:网站首页>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;
} 边栏推荐
- [research materials] 2022q1 game preferred casual game distribution circular - Download attached
- Symbol of array element product of leetcode simple problem
- Notes | numpy-07 Slice and index
- Valentine's day limited withdrawal guide: for one in 200 million of you
- Market status and development prospect prediction of global SoC Test Platform Industry in 2022
- Notes | numpy-10 Iterative array
- Introduction to message queuing (MQ)
- What is UUID
- MySQL winter vacation self-study 2022 12 (3)
- Introduction to JVM principle
猜你喜欢

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

RT thread flow notes I startup, schedule, thread

Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution

ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)

论文阅读_中文医疗模型_ eHealth

Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly

Symbol of array element product of leetcode simple problem
![[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)](/img/2a/362f3b0491f721d89336d4f468c9dd.jpg)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)

Oracle SQL table data loss

Retirement plan fails, 64 year old programmer starts work again
随机推荐
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
《牛客刷verilog》Part II Verilog进阶挑战
[BMZCTF-pwn] 18-RCTF-2017-Recho
STM32 reverse entry
Market status and development prospects of the global IOT active infrared sensor industry in 2022
MediaTek 2023 IC written examination approved in advance (topic)
Network security textual research recommendation
Shuttle + alluxio accelerated memory shuffle take-off
What is UUID
Apache MPM model and ab stress test
General undergraduate college life pit avoidance Guide
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
Objects. Requirenonnull method description
Valentine's day limited withdrawal guide: for one in 200 million of you
Notes | numpy-10 Iterative array
[luatos sensor] 2 air pressure bmp180
Wechat applet distance and map
[USACO 2009 Dec S]Music Notes