直捣黄龙(Dijkstra (30 分)

本文介绍了一种使用Dijkstra算法解决的军事策略问题,通过地图上的节点和边权重计算从起始城市到目标城市的最短路径,同时考虑敌方城市防御和杀伤力。算法优化了路径选择,注重士兵数量、城市攻占次数和敌人杀伤量的综合评估。
摘要由CSDN通过智能技术生成

在这里插入图片描述
在这里插入图片描述
题目链接

AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int N = 210;
int a[N];
int n, m;
string st, ed;
map <string,int> mp;
map <int, string> mp2;
int enemy[N], g[N][N], dis[N], city[N], kill[N], vis[N], num[N];
int pre[N];
vector <int> path;
void dijkstra ()
{
	memset (dis, 0x3f, sizeof dis);
	dis[mp[st]] = 0;
	num[mp[st]] = 1;
	for (int i = 1; i <= n; i ++)
	{
		int t = -1;
		for (int j = 1; j <= n; j ++)
		{
			if (!vis[j] && (t == -1 || dis[j] < dis[t]))
				t = j;
		}
		if (i == 1) t = mp[st];	
		vis[t] = 1;
		for (int j = 1; j <= n; j ++)
		{
			if (dis[j] > dis[t] + g[t][j])
			{
				num[j] = num[t];
				dis[j] = dis[t] + g[t][j];
				kill[j] = kill[t] + enemy[j];
				city[j] = city[t] + 1;
				pre[j] = t;
			}
			else if (dis[j] == dis[t] + g[t][j])
			{
				num[j] += num[t];
				if (city[j] < city[t] + 1)
				{
					city[j] = city[t] + 1;
					kill[j] = kill[t] + enemy[j];
					pre[j] = t;
				}
				else if (city[j] == city[t] + 1)
				{
					if (kill[j] < kill[t] + enemy[j])
					{
						kill[j] = kill[t] + enemy[j];
						pre[j] = t;
					}
				}
			}		
		}
	} 	
}
int main ()
{
	ios::sync_with_stdio (false);
	cin.tie (0); cout.tie (0);
	cin >> n >> m >> st >> ed;
	int cnt = 1;
	mp2[cnt] = st;
	mp[st] = cnt ++;
	for (int i = 1; i < n; i ++)
	{
		string s; cin >> s;
		mp2[cnt] = s;
		mp[s] = cnt ++;
		int x; cin >> x;
		enemy[mp[s]] = x;
	}
	memset (g, 0x3f, sizeof g);
	while (m --)
	{
		string s1, s2; int x;
		cin >> s1 >> s2 >> x;
		int a = mp[s1], b = mp[s2];
		g[a][b] = g[b][a] = x;
	}
	dijkstra ();
	
	int a = mp[ed];
	while (a != mp[st])
	{
		path.push_back(a);
		a = pre[a];
	}
	path.push_back(mp[st]);
	reverse (path.begin(), path.end ());
	for (int i = 0; i < (int)path.size(); i ++)
	{
		if (i == 0)
			cout << mp2[path[i]];
		else 
			cout << "->" << mp2[path[i]];
	}
	cout << "\n";
	cout << num[mp[ed]] << " " << dis[mp[ed]] << " " << kill[mp[ed]];

	return 0;	
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值