题目要求
本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营。首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营。当这样的路径不唯一时,要求选择可以沿途解放最多城镇的路径。若这样的路径也不唯一,则选择可以有效杀伤最多敌军的路径。
输入格式:
输入第一行给出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
代码
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define endl "\n"
const int INF=2147483647;
int n,m,num;//城镇数 道路数
string be,ed,s;//起点 终点 城镇
struct edge{
string v;
int w;
};
map<string,vector<edge>>e;//存图
//最短距离 是否访问 城镇人数 解放城镇数量 杀敌数 最短路径条数
map<string,int>d,vis,man,city,people,shortpath;
map<string,string>pre;//保存前驱
unordered_set<string>st;//存有什么地点
priority_queue<pair<int,string>>q;
void dijkstra(string s){
for(auto i:st) d[i]=INF;
d[s]=0;q.push({0,s});
shortpath[s]=1;
while(q.size()){
auto t=q.top();q.pop();
string u=t.second;
if(vis[u]) continue;
vis[u]=1;
for(auto ed:e[u])
{
string v=ed.v;
int w=ed.w;
if(d[v]>d[u]+w){//距离要最短
d[v]=d[u]+w;
city[v]=city[u]+1;
people[v]=people[u]+man[v];
shortpath[v]=shortpath[u];
q.push({-d[v],v});
pre[v]=u;
}else if(d[v]==d[u]+w){//最短路径不唯一时
shortpath[v]+=shortpath[u];
if(city[v]<city[u]+1){//解放城镇要最多
city[v]=city[u]+1;
people[v]=people[u]+man[v];
pre[v]=u;
}else if(city[v]==city[u]+1){
if(people[v]<people[u]+man[v]){//杀敌要最多
people[v]=people[u]+man[v];
pre[v]=u;
}
}
}
}
}
}
vector<string>path;
void getpath(string s)//获取最短路径
{
while(s!="000"){
path.pb(s);
s=pre[s];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m>>be>>ed;
pre[be]="000";//将起点前驱定为000
st.insert(be);
for(int i=1;i<=n-1;i++)
{
cin>>s>>num;
st.insert(s);
man[s]=num;
}
for(int i=0;i<m;i++)
{
string u,v;
int w;
cin>>u>>v>>w;
e[u].pb({v,w});
e[v].pb({u,w});
}
dijkstra(be);
getpath(ed);
for(int i=path.size()-1;i>=0;i--)
{
if(i!=path.size()-1) cout<<"->";
cout<<path[i];
}
cout<<endl;
cout<<shortpath[ed]<<" "<<d[ed]<<" "<<people[ed];
return 0;
}
该文章描述了一个基于C++的程序,利用Dijkstra算法寻找从己方大本营到敌方大本营的最优攻击路径,考虑了路径速度、解放城镇数量和杀敌数量等因素。程序通过构建图结构,设置优先队列进行最短路径搜索,并在路径不唯一时依据特定条件选择最佳路径。
3138

被折叠的 条评论
为什么被折叠?



