当前位置:网站首页>L3-011 直捣黄龙 (30 分)
L3-011 直捣黄龙 (30 分)
2022-04-23 03:22:00 【再敲一行就去睡】
本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营。首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营。当这样的路径不唯一时,要求选择可以沿途解放最多城镇的路径。若这样的路径也不唯一,则选择可以有效杀伤最多敌军的路径。
输入格式:
输入第一行给出 2 个正整数 N(2 ≤ N ≤ 200,城镇总数)和 K(城镇间道路条数),以及己方大本营和敌方大本营的代号。随后 N-1 行,每行给出除了己方大本营外的一个城镇的代号和驻守的敌军数量,其间以空格分隔。再后面有 K 行,每行按格式城镇1 城镇2 距离给出两个城镇之间道路的长度。这里设每个城镇(包括双方大本营)的代号是由 3 个大写英文字母组成的字符串。
输出格式:
按照题目要求找到最合适的进攻路径(题目保证速度最快、解放最多、杀伤最强的路径是唯一的),并在第一行按照格式己方大本营->城镇1->...->敌方大本营输出。第二行顺序输出最快进攻路径的条数、最短进攻距离、歼敌总数,其间以 1 个空格分隔,行首尾不得有多余空格。
输入样例:
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
输出样例:
PAT->PTA->PDS->DBY
3 30 210
综合性较强的图论题,本解法特点如下
1.使用两个map作为哈希表
2.初始化所有节点为最大距离,使其可作为访问数组使用
3.未使用Djkstra算法,而是队列访问,每次刷新数据均将节点重新入队,否则不再入队
#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; //字符到数字的映射
map<int,string> mi; //数字到字符的映射
m[st]=0;
mi[0]=st;
int ps[n]; //城市守军人数
int dis[n];fill(dis,dis+n,0x7fffffff); //距离,初始为最大
int path[n]; //路径
int rs[n]={0}; //路径条数
int sav[n]={0}; //杀伤人数
int tot[n]={0}; //路过城市数
vector<int> p[n]; //邻接表
t=(int**)malloc(sizeof(int*)*n); //邻接矩阵,存放道路长度
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; //图访问队列
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]]){ //距离更近
dis[p[x][i]]=dis[x]+t[x][p[x][i]]; //距离
path[p[x][i]]=x; //路径
rs[p[x][i]]=rs[x]; //路径条数
sav[p[x][i]]=sav[x]+ps[p[x][i]]; //杀伤人数
tot[p[x][i]]=tot[x]+1; //路过城市数
f.push(p[x][i]); //入队
}
else if(dis[x]+t[x][p[x][i]]==dis[p[x][i]]){ //距离相等时
rs[p[x][i]]=0; //最短路径数必须重新统计
for(int j=0;j<p[p[x][i]].size();j++){ //遍历出同为最短路径的城市并统计
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){ //城市数更少
path[p[x][i]]=x; //路径
sav[p[x][i]]=sav[x]+ps[p[x][i]]; //杀伤人数
tot[p[x][i]]=tot[x]+1; //路过城市数
f.push(p[x][i]); //入队
}
else if(tot[p[x][i]]==tot[x]+1){ //城市相等时
if(sav[p[x][i]]<sav[x]+ps[p[x][i]]){ //杀伤人数更少
path[p[x][i]]=x; //路径
sav[p[x][i]]=sav[x]+ps[p[x][i]]; //杀伤人数
f.push(p[x][i]); //入队
}
}
}
}
}
stack<int> ot; //路径输出栈
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;
}

版权声明
本文为[再敲一行就去睡]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46212625/article/details/124357823
边栏推荐
- Supersocket is Used in net5 - command
- The most understandable life cycle of dependency injection
- 2022g2 boiler stoker examination question bank and online simulation examination
- Fiddler use
- Use of ADB command [1]
- Experiment 5 components and event handling
- 12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
- 《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
- 集合之List接口
- Scenario Title: how does system a use the page of system B
猜你喜欢
![Use of ADB command [1]](/img/e6/fb8fd6956c226f75b831f8eb46277f.png)
Use of ADB command [1]

Blazor University (12) - component lifecycle

Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis

2022t elevator repair test simulation 100 questions and online simulation test

AWS from entry to actual combat: creating accounts

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

A comprehensive understanding of static code analysis
![General testing technology [1] classification of testing](/img/f1/d80b6793b6443cbc4048d7e6319f51.png)
General testing technology [1] classification of testing
![[vs Code] solve the problem that the jupyter file displays exceptions in vs code](/img/f6/a91d03fd140eb5f7688b72e2e6f2bb.png)
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
随机推荐
General testing technology [1] classification of testing
Optimization of especially slow startup in idea debugging mode
2022年P气瓶充装培训试题及模拟考试
批量下载文件----压缩后再下载
The most easy to understand service container and scope of dependency injection
. NETCORE sets the API post mode, which can accept parameters directly in parentheses
. net webapi access authorization mechanism and process design (header token + redis)
Eight elder brothers chronicle [4]
IDEA查看历史记录【文件历史和项目历史】
[untitled]
12.<tag-链表和常考点综合>-lt.234-回文链表
js递归树结构计算每个节点的叶子节点的数量并且输出
Supersocket is Use in net5 - startup
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
Generate QR code through zxing
Experiment 5 components and event handling
This new feature of C 11, I would like to call it the strongest!
MySQL query specifies that a row is sorted to the first row
Comprehensive calculation of employee information
2022t elevator repair test simulation 100 questions and online simulation test