UVA - 1416 Warfare And Logistics (最短路)

本文介绍了一种通过移除特定边来最大化最短路径总成本的算法,利用Dijkstra算法进行实现,并通过示例展示了如何计算原始及优化后的总成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Download as PDF

The army of United Nations launched a new wave of air strikes on terroristforces. The objective of the mission is to reduce enemy's logistical mobility. Each airstrike will destroy a path and therefore increase the shipping cost of the shortest pathbetween two enemy locations. The maximal damage is always desirable.

Let's assume that there are n enemy locations connected by m bidirectional paths,each with specific shipping cost. Enemy's total shipping cost is given as c = $ \sum^{​{n}}_{​{i=1}}$$ \sum^{​{n}}_{​{j=1}}$path(i, j). Here path(i, j) is the shortest path between locations i and j. In case i and j are not connected, path(i, j) = L. Each air strike can only destroy one path. The total shipping cost after the strike is noted as c'. In order to maximizedthe damage to the enemy, UN's air force try to find the maximal c' - c.

Input

The first line ofeach input case consists ofthree integers: n, m, and L. 1 < n$ \le$100,1$ \le$m$ \le$1000, 1$ \le$L$ \le$10$\scriptstyle \wedge$8. Each ofthe following m lines contains three integers: a,b, s, indicating length of the path between a and b.

Output

For each case, output the total shipping cost before the air strike and the maximaltotal shipping cost after the strike. Output them in one line separated by a space.

Sample Input

4  6  1000
1  3  2
1  4  4
2  1  3
2  3  3
3  4  1
4  2  2

Sample Output

28  38

题意:给出一个n节点m条边的无向图,每条边上有一个正权,令c等于每对结点的最短路径之和,要求删除一条边后使得新的c值最大。不连通的两个点距离视为L

思路:每次求单源最短路的同一时候记录下删除边的值,然后计算最小值就是了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 1010;
const int INF = 0x3f3f3f3f;

struct Edge {
	int from, to, dist;
};

struct HeapNode {
	int d, u;
	bool operator<(const HeapNode rhs) const {
		return d > rhs.d;
	}
};

struct Dijkstra {
	int n, m;
	vector<Edge> edges;
	vector<int> G[MAXN];
	bool done[MAXN];
	ll d[MAXN];
	int p[MAXN];

	void init(int n) {
		this->n = n;
		for (int i = 0; i < n; i++)
			G[i].clear();
		edges.clear();
	}

	void AddEdge(int from, int to, int dist) {
		edges.push_back((Edge){from, to, dist});
		edges.push_back((Edge){to, from, dist});
		m = edges.size();
		G[from].push_back(m-2);
		G[to].push_back(m-1);
	}

	void dijkstra(int s, int curEdge) {
		priority_queue<HeapNode> Q;
		for (int i = 0; i < n; i++)
			d[i] = INF;
		d[s] = 0;
		memset(done, 0, sizeof(done));
		memset(p, -1, sizeof(p));
		Q.push((HeapNode){0, s});
		while (!Q.empty()) {
			HeapNode x = Q.top();
			Q.pop();
			int u = x.u;
			if (done[u])
				continue;
			done[u] = true;
			for (int i = 0; i < G[u].size(); i++) {
				Edge &e = edges[G[u][i]];
				if (G[u][i] == curEdge/2*2 || G[u][i] == curEdge/2*2+1)
					continue;
				if (d[e.to] > d[u] + e.dist) {
					d[e.to] = d[u] + e.dist;
					p[e.to] = G[u][i];
					Q.push((HeapNode){d[e.to], e.to});
				}
			}
		}
	}
}arr;

int pre[MAXN];
int n, m, L;
ll s[MAXN][MAXN], A[MAXN];

int main() {
	while (scanf("%d%d%d", &n, &m, &L) != EOF) {
		int u, v, w;
		arr.init(n);			
		for (int i = 0; i < m; i++) {
			scanf("%d%d%d", &u, &v, &w);
			u--, v--;
			arr.AddEdge(u, v, w);
		}
		ll ans = 0;
		memset(s, -1, sizeof(s));
		for (int i = 0; i < n; i++) {
			arr.dijkstra(i, -100);
			ll sum = 0;
			for (int j = 0; j < n; j++) {
				if (arr.d[j] == INF)
					arr.d[j] = L;
				sum += arr.d[j];
				pre[j] = arr.p[j];			
			}
			A[i] = sum;
			ans += sum;
			for (int j = 0; j < n; j++) {
				if (j == i || pre[j] == -1)
					continue;
				ll tmp = 0;
				arr.dijkstra(i, pre[j]);
				for (int k = 0; k < n; k++) {
					if (arr.d[k] == INF)
						arr.d[k] = L;
					tmp += arr.d[k];
				}
				s[pre[j]/2][i] = tmp;
			}
		}
		ll Max = 0;
		for (int i = 0; i < m; i++) {
			ll sum = 0;
			for (int j = 0; j < n; j++)
				if (s[i][j] == -1)
					sum += A[j];
				else sum += s[i][j];
			Max = max(Max, sum);
		}
		printf("%lld %lld\n", ans, Max);
	}
	return 0;
}






资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持多种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问题。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个项目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值