当前位置:网站首页>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
边栏推荐
- 二进制文件版本控制工具选择难?看完这篇你会找到答案
- Fundamentals of software testing and development
- Huawei mobile ADB devices connection device is empty
- oracle 查询外键含有逗号分隔的数据
- Super easy to use asynchronous export function of Excel
- MySql分组查询规则
- Build websocket server in. Net5 webapi
- Test experience data
- Cefsharp stores cookies and reads cookies
- . net webapi access authorization mechanism and process design (header token + redis)
猜你喜欢
2022g2 boiler stoker examination question bank and online simulation examination
General test technology [II] test method
Use of ADB command [1]
Charles uses three ways to modify requests and responses
Configuration table and page information automatically generate curd operation page
Supersocket is Use in net5 - concept
Iotos IOT middle platform is connected to the access control system of isecure center
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
Web Course Design - his system
A comprehensive understanding of static code analysis
随机推荐
TCP three handshakes and four waves
第四次作业
ASP. Net 6 middleware series - conditional Middleware
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
socket編程 send()與 recv()函數詳解
一套组合拳,打造一款 IDEA 护眼方案
The query type of MySQL is very inefficient.
Preview of converting doc and PDF to SWF file
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
Docker pulls MySQL and connects
WinForm allows the form form to switch between the front and active states
Comprehensive calculation of employee information
通过 zxing 生成二维码
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
Docker拉取mysql并连接
JS implementation of new
全新的ORM框架——BeetlSQL介绍
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
. NETCORE sets the API post mode, which can accept parameters directly in parentheses
2022a special equipment related management (elevator) work license question bank and simulation examination