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 (最短路)的更多相关文章
- uva 1416 Warfare And Logistics
题意: 给出一个无向图,定义这个无向图的花费是 其中path(i,j),是i到j的最短路. 去掉其中一条边之后,花费为c’,问c’ – c的最大值,输出c和c’. 思路: 枚举每条边,每次把这条边去掉 ...
- UVA 4080 Warfare And Logistics 战争与物流 (最短路树,变形)
题意: 给一个无向图,n个点,m条边,可不连通,可重边,可多余边.两个问题,第一问:求任意点对之间最短距离之和.第二问:必须删除一条边,再求第一问,使得结果变得更大. 思路: 其实都是在求最短路的过程 ...
- UVA1416 Warfare And Logistics
UVA1416 Warfare And Logistics 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36232 [ ...
- Warfare And Logistics UVA - 1416
题目链接:https://vjudge.net/problem/UVA-1416 题解: 这是一个最短路的好题,首先我们考虑如果暴力弗洛伊德,显然时间复杂度不对,如果做n次spfa好像复杂度也不对,所 ...
- 【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条无向边的图,每条边权都为正.令 ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVALive 4080 Warfare And Logistics (最短路树)
很多的边会被删掉,需要排除一些干扰进行优化. 和UVA - 1279 Asteroid Rangers类似,本题最关键的地方在于,对于一个单源的最短路径来说,如果最短路树上的边没有改变的话,那么最短路 ...
- UVa 1599 (字典序最小的最短路) Ideal Path
题意: 给出一个图(图中可能含平行边,可能含环),每条边有一个颜色标号.在从节点1到节点n的最短路的前提下,找到一条字典序最小的路径. 分析: 首先从节点n到节点1倒着BFS一次,算出每个节点到节点n ...
- LA4080/UVa1416 Warfare And Logistics 最短路树
题目大意: 求图中两两点对最短距离之和 允许你删除一条边,让你最大化删除这个边之后的图中两两点对最短距离之和. 暴力:每次枚举删除哪条边,以每个点为源点做一次最短路,复杂度\(O(NM^2logN)\ ...
随机推荐
- windows基本DOS命令
基本命令 dir : 列出当前目录下的文件以及文件夹,后面可以接其他路径 md : 创建目录(mkdir),一次创建多级目录,mkdir a\b\c rd : 删除目录,删除非空目录rd /s(删除最 ...
- windows系统使用的误区
1.软件不要装在c盘,影响系统运行速度. 软件c盘不会影响系统运行,会提高软件运行速度 PS:前提条件:C盘有足够的空余空间(50G以上). 大多数软件安装默认路径在C盘的原因是不知道电脑分区的情况, ...
- WebApp开发入门
web app 的技术平台很多,如adobe phonegap.sencha touch.appcan(国产).dcloud(国产)平台.我选择了dcloud平台,原因:简单,容易上手. web ap ...
- install chrome on ubuntu14.04
summary chrome broswer can't found in ubuntu14.04 default source list.To install chrome ,you must ad ...
- 用Javascript实现图片的缓慢缩放效果
<body> <!--页面布局:一张图片两个按钮--> <div style = "width:400px;margin:0 auto"> &l ...
- HDU-1853 Cyclic Tour
最小权值环覆盖问题:用几个环把所有点覆盖,求所选取的边最小的权值之和. 拆点思想+求最小转求最大+KM算法 #include <cstdlib> #include <cstdio&g ...
- 洛谷P2664 树上游戏 【点分治 + 差分】
题目 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] 输入格式 第一行为一个整数n,表示树节点的数量 ...
- 解决 Could not load hsdis-amd64.dll
win10下想查看JIT编译的汇编源码 结果提示: Could not load hsdis-amd64.dll; library not loadable; PrintAssembly is dis ...
- 事务的传播行为和隔离级别[transaction behavior and isolated level]
Spring中事务的定义:一.Propagation : key属性确定代理应该给哪个方法增加事务行为.这样的属性最重要的部份是传播行为.有以下选项可供使用: PROPAGATION_REQUIRED ...
- Linq技巧1——关联实体查询排序
假如想查询拖欠按揭超过30天的银行帐号,同时查询出他们的单据,并且需要按照单据日期进行排序,这样可以首先看到最近的单据,方便找出问题. 大多数人都知道EF可以使用Include()热加载关系实体,例如 ...