UVA - 1416 Warfare And Logistics (最短路)

Description

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 = 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 < n100,1m1000,
1L108.
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;
}

UVA - 1416 Warfare And Logistics (最短路),布布扣,bubuko.com

时间: 2025-01-04 10:12:16

UVA - 1416 Warfare And Logistics (最短路)的相关文章

uva 1416 Warfare And Logistics (最短路树)

uva 1416 Warfare And Logistics Description The army of United Nations launched a new wave of air strikes on terrorist forces. The objective of the mission is to reduce enemy's logistical mobility. Each air strike will destroy a path and therefore inc

UVA 1416 - Warfare And Logistics(最短路树)

UVA 1416 - Warfare And Logistics 题目链接 题意:给定一个无向图,每个边一个正权,c等于两两点最短路长度之和,现在要求删除一边之后,新图的c值最大的是多少 思路:直接枚举删边,每次做一次dijkstra的话复杂度太高,其实如果建好最短路树,如果删去的边在最短路树上,才需要去做,这样复杂度就优化到(n^2mlog(n)),勉强可以接受 代码: #include <cstdio> #include <cstring> #include <vecto

UVA 4080 Warfare And Logistics 战争与物流 (最短路树,变形)

题意:给一个无向图,n个点,m条边,可不连通,可重边,可多余边.两个问题,第一问:求任意点对之间最短距离之和.第二问:必须删除一条边,再求第一问,使得结果变得更大. 思路: 其实都是在求最短路的过程. 第一问可以floyd解决,也可以SSSP解决.注意是任意两个点,(a,b)和(b,a)是不同的,都要算. 第二问要穷举删除每条边,再求第一问.为了降低复杂度,假设用dijkstra求最短路,那么可以利用第一问中所生成的树,共n棵,每棵至多n-1条边,如果穷举的边不在该某树上,那么该树的所有路径长不

Warfare And Logistics UVALive - 4080 (最短路树)

Warfare And Logistics UVALive - 4080 题意:给n个点m条边.令c为每对节点的最短路长度之和.要求删除一条边后使得新的c值c'最大,不连通的两点对短路视为L. [如果每次删除一条边,要跑m次dijkstra,其实其中很多次都对最短路没有影响,因为删掉的边不在最短路里] [因此,可以每次删除最短路树中的一条边,需要跑n次,复杂度降低到可接受程度] 1 #include <bits/stdc++.h> 2 #define LL long long 3 using

UVA 1416 最短路树

Warfare And Logistics 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

UVA 116 Unidirectional TSP(DP最短路字典序)

Description  Unidirectional TSP  Background Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Travel

UVA - 10537 The Toll! Revisited (最短路变形逆推)

Description Problem G Toll! Revisited Input: Standard Input Output: Standard Output Time Limit: 1 Second Sindbad the Sailor sold 66 silver spoons to the Sultan of Samarkand. The selling was quite easy; but delivering was complicated. The items were t

Warfare And Logistics UVA - 1416

题目链接:https://vjudge.net/problem/UVA-1416 题解: 这是一个最短路的好题,首先我们考虑如果暴力弗洛伊德,显然时间复杂度不对,如果做n次spfa好像复杂度也不对,所以考虑优化这个暴力. 我们考虑对于一个单源最短路,只有改变了最短路树中的某条边,才需要重新做一次最短路.所以我们不需要对于每条边都重新做最短路,只需要对于在最短路数上的边做,所以时间复杂度就优化成了你] mn^2log(n). 实现的时候要用pre数组记下,以i为终点的最短路树的边,实现有点复杂,看

【UVA1416】(LA4080) Warfare And Logistics (单源最短路)

题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令c等于每对结点的最短路长度之和.要求删一条边后使得新的c值c‘最大.不连通两点的最短路长度视为L.(1<=n<=100,1<=m<=1000,1<=L<=10^8) 分析: 因为规模比较小,所以可以考虑删边.主要是删什么边的问题. 这里用到最短路树.在源点确定的情况下,只要