当前位置:网站首页>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
边栏推荐
- Knowledge of software testing~
- 超好用的Excel异步导出功能
- Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
- Fiddler use
- 全新的ORM框架——BeetlSQL介绍
- 2022年做跨境电商五大技巧小分享
- . net core current limiting control - aspnetcoreratelimit
- Xamarin effect Chapter 22 recording effect
- 手机连接电脑后,QT的QDIR怎么读取手机文件路径
- 《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
猜你喜欢

IDEA查看历史记录【文件历史和项目历史】

xutils3修改了我提报的一个bug,开心

12.<tag-链表和常考点综合>-lt.234-回文链表
![[mock data] fastmock dynamically returns the mock content according to the incoming parameters](/img/1e/c4aad49d16fb21a19865ba75128e43.png)
[mock data] fastmock dynamically returns the mock content according to the incoming parameters

Quartz. Www. 18fu Used in net core

Iotos IOT middle platform is connected to the access control system of isecure center

Explanation keyword of MySQL

Node configuration environment CMD does not take effect

It can receive multiple data type parameters - variable parameters

MySql关键字GROUP_CONCAT,组合连接查询
随机推荐
C-11 problem I: find balloon
批量下载文件----压缩后再下载
New ORM framework -- Introduction to beetlsql
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
Charles uses three ways to modify requests and responses
【无标题】
关于idea调试模式下启动特别慢的优化
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
This new feature of C 11, I would like to call it the strongest!
Top 9 task management system in 2022
幂等性实践操作,基于业务讲解幂等性
Idempotency practice operation, explaining idempotency based on business
Super easy to use asynchronous export function of Excel
socket編程 send()與 recv()函數詳解
软件测试相关知识~
Explication détaillée des fonctions send () et recv () du programme Socket
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
EasyUI's combobox implements three-level query
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
Comprehensive calculation of employee information