当前位置:网站首页>L3-011 direct attack Huanglong (30 points)
L3-011 direct attack Huanglong (30 points)
2022-04-23 03:23:00 【One more line and go to bed】
This is a war movie —— You need to start from your own base , All the way to the enemy's base camp . First of all, time is life , So you have to choose the right path , Capture the enemy base camp as quickly as possible . When such a path is not unique , It is required to choose the path that can liberate the most towns along the way . If such a path is not unique , Then select the path that can effectively kill the most enemy .
Input format :
The first line of input is given 2 A positive integer N(2 ≤ N ≤ 200, Total number of towns ) and K( Number of roads between cities and towns ), And the codes of your base camp and enemy base camp . And then N-1 That's ok , Each line gives the code of a town other than your base camp and the number of enemy troops stationed , Separated by spaces . Then there is K That's ok , Each line is formatted Cities and towns 1 Cities and towns 2 distance
Give the length of the road between two towns . There are every town here ( Including the bases of both sides ) The code name is by 3 A string of uppercase letters .
Output format :
Find the most suitable attack path according to the requirements of the topic ( The title guarantees the fastest speed 、 Liberation most 、 The strongest path to kill is the only ), And in the first line according to the format Your base camp -> Cities and towns 1->...-> Enemy stronghold
Output . The second line sequentially outputs the number of fastest attack paths 、 The shortest attack distance 、 The total number of enemy annihilations , In the meantime 1 Space separation , There must be no extra space at the beginning and end of the line .
sample input :
10 12 PAT DBY
DBY 100
PTA 20
PDS 90
PMS 40
TAP 50
ATP 200
LNN 80
LAO 30
LON 70
PAT PTA 10
PAT PMS 10
PAT ATP 20
PAT LNN 10
LNN LAO 10
LAO LON 10
LON DBY 10
PMS TAP 10
TAP DBY 10
DBY PDS 10
PDS PTA 10
DBY ATP 10
sample output :
PAT->PTA->PDS->DBY
3 30 210
Graph topics with strong comprehensiveness , The characteristics of this solution are as follows
1. Use two map As a hash table
2. Initialize all nodes to the maximum distance , Make it available as an access array
3. not used Djkstra Algorithm , It's queue access , Each time the data is refreshed, the node is re queued , Or you won't join the team anymore
#include <bits/stdc++.h>
using namespace std;
int main(void){
int n,k,tar,**t;
string st,ed;
cin>>n>>k>>st>>ed;
map<string,int> m; // Character to number mapping
map<int,string> mi; // Number to character mapping
m[st]=0;
mi[0]=st;
int ps[n]; // Number of city defenders
int dis[n];fill(dis,dis+n,0x7fffffff); // distance , The initial is the maximum
int path[n]; // route
int rs[n]={0}; // Number of paths
int sav[n]={0}; // Number of casualties
int tot[n]={0}; // Number of passing cities
vector<int> p[n]; // Adjacency list
t=(int**)malloc(sizeof(int*)*n); // Adjacency matrix , Length of storage road
for(int i=0;i<n;i++){
t[i]=(int*)malloc(sizeof(int)*n);
for(int j=0;j<n;j++)t[i][j]=0;
}
for(int i=1;i<n;i++){
string x;
int c;
cin>>x>>c;
m[x]=i;
mi[i]=x;
ps[i]=c;
}
tar=m[ed];
while(k--){
string xt,yt;
int d,x,y;
cin>>xt>>yt>>d;
x=m[xt];
y=m[yt];
p[x].push_back(y);
p[y].push_back(x);
t[x][y]=d;
t[y][x]=d;
}
queue<int> f; // Figure access queue
f.push(0);
path[0]=-1;
dis[0]=0;
rs[0]=1;
while(!f.empty()){
int x=f.front();f.pop();
for(int i=0;i<p[x].size();i++){
if(dis[x]+t[x][p[x][i]]<dis[p[x][i]]){ // Closer
dis[p[x][i]]=dis[x]+t[x][p[x][i]]; // distance
path[p[x][i]]=x; // route
rs[p[x][i]]=rs[x]; // Number of paths
sav[p[x][i]]=sav[x]+ps[p[x][i]]; // Number of casualties
tot[p[x][i]]=tot[x]+1; // Number of passing cities
f.push(p[x][i]); // The team
}
else if(dis[x]+t[x][p[x][i]]==dis[p[x][i]]){ // When the distance is equal
rs[p[x][i]]=0; // The number of shortest paths must be counted again
for(int j=0;j<p[p[x][i]].size();j++){ // Traverse the cities with the shortest path and count
if(dis[p[x][i]]==dis[p[p[x][i]][j]]+t[p[x][i]][p[p[x][i]][j]])
rs[p[x][i]]+=rs[p[p[x][i]][j]];
}
if(tot[p[x][i]]<tot[x]+1){ // Fewer cities
path[p[x][i]]=x; // route
sav[p[x][i]]=sav[x]+ps[p[x][i]]; // Number of casualties
tot[p[x][i]]=tot[x]+1; // Number of passing cities
f.push(p[x][i]); // The team
}
else if(tot[p[x][i]]==tot[x]+1){ // When cities are equal
if(sav[p[x][i]]<sav[x]+ps[p[x][i]]){ // Fewer casualties
path[p[x][i]]=x; // route
sav[p[x][i]]=sav[x]+ps[p[x][i]]; // Number of casualties
f.push(p[x][i]); // The team
}
}
}
}
}
stack<int> ot; // Path output stack
int tt=tar;
while(tt!=-1){
ot.push(tt);
tt=path[tt];
}
while(!ot.empty()){
int as=ot.top();
cout<<mi[as];
ot.pop();
if(!ot.empty())cout<<"->";
}
cout<<endl<<rs[tar]<<' '<<dis[tar]<<' '<<sav[tar];
return 0;
}
版权声明
本文为[One more line and go to bed]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230322232730.html
边栏推荐
- MySql分组查询规则
- 浅学一下I/O流和File类文件操作
- Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]
- Supersocket is Use in net5 - concept
- Preview of converting doc and PDF to SWF file
- 二进制文件版本控制工具选择难?看完这篇你会找到答案
- poi根据数据创建导出excel
- Five tips for cross-border e-commerce in 2022
- Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
- Iotos IOT middle platform is connected to the access control system of isecure center
猜你喜欢
全新的ORM框架——BeetlSQL介绍
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
L3-011 直捣黄龙 (30 分)
为什么BI对企业这么重要?
IDEA查看历史记录【文件历史和项目历史】
Is it difficult to choose binary version control tools? After reading this article, you will find the answer
随机推荐
Iotos IOT middle platform is connected to the access control system of isecure center
Node configuration environment CMD does not take effect
Using swagger in. Net5
二进制文件版本控制工具选择难?看完这篇你会找到答案
Log4net is in Net core usage
Test questions (2)
Fiddler use
关于idea调试模式下启动特别慢的优化
Peut recevoir plusieurs paramètres de type de données - paramètres variables
【VS Code】解决jupyter文件在vs code中显示异常的问题
Huawei mobile ADB devices connection device is empty
WinForm allows the form form to switch between the front and active states
General testing technology [1] classification of testing
C-10 program error correction (recursive function): number to character
打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
Web Course Design - his system
第四次作业
Idea view history [file history and project history]
2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)
Supersocket is Use in net5 - concept