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 (最短路)的更多相关文章

  1. uva 1416 Warfare And Logistics

    题意: 给出一个无向图,定义这个无向图的花费是 其中path(i,j),是i到j的最短路. 去掉其中一条边之后,花费为c’,问c’ – c的最大值,输出c和c’. 思路: 枚举每条边,每次把这条边去掉 ...

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

    题意: 给一个无向图,n个点,m条边,可不连通,可重边,可多余边.两个问题,第一问:求任意点对之间最短距离之和.第二问:必须删除一条边,再求第一问,使得结果变得更大. 思路: 其实都是在求最短路的过程 ...

  3. UVA1416 Warfare And Logistics

    UVA1416 Warfare And Logistics 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36232 [ ...

  4. Warfare And Logistics UVA - 1416

    题目链接:https://vjudge.net/problem/UVA-1416 题解: 这是一个最短路的好题,首先我们考虑如果暴力弗洛伊德,显然时间复杂度不对,如果做n次spfa好像复杂度也不对,所 ...

  5. 【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条无向边的图,每条边权都为正.令 ...

  6. UVA 11374 Airport Express(最短路)

    最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...

  7. UVALive 4080 Warfare And Logistics (最短路树)

    很多的边会被删掉,需要排除一些干扰进行优化. 和UVA - 1279 Asteroid Rangers类似,本题最关键的地方在于,对于一个单源的最短路径来说,如果最短路树上的边没有改变的话,那么最短路 ...

  8. UVa 1599 (字典序最小的最短路) Ideal Path

    题意: 给出一个图(图中可能含平行边,可能含环),每条边有一个颜色标号.在从节点1到节点n的最短路的前提下,找到一条字典序最小的路径. 分析: 首先从节点n到节点1倒着BFS一次,算出每个节点到节点n ...

  9. LA4080/UVa1416 Warfare And Logistics 最短路树

    题目大意: 求图中两两点对最短距离之和 允许你删除一条边,让你最大化删除这个边之后的图中两两点对最短距离之和. 暴力:每次枚举删除哪条边,以每个点为源点做一次最短路,复杂度\(O(NM^2logN)\ ...

随机推荐

  1. PHP mac xdebug配置

    PHP实现断点调试的条件 1. 需要PHP安装xdebug扩展 2. 修改PHP配置文件,开启xdebug扩展,并且对xdebug进行一些配置 3. 重启服务器如apach或nginx 4. 编译器配 ...

  2. Codeforces Round #412 Div. 2 第一场翻水水

    大半夜呆在机房做题,我只感觉智商严重下降,今天我脑子可能不太正常 A. Is it rated? time limit per test 2 seconds memory limit per test ...

  3. php 审批流程管理

    1.流程管理的用法是什么样的? 2.怎么发起想要的流程? 3.审批的人要是怎么审批通过? 4.流程审核是不是要挨个走过? 一.要有数据库的内容的 肯定会有表的,首先就是用户表了,然后就是流程表,用户编 ...

  4. 【bzoj3207】花神的嘲讽计划Ⅰ Hash+STL-map+莫队算法

    题目描述 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天DJ在给吾等众蒟 ...

  5. UVa——400Unix ls(字典序文本处理输出iomanip)

    Unix ls Time Limit:                                                        3000MS                    ...

  6. el-select绑定值为对象时,报错[Vue warn]: <transition-group> children must be keyed: <ElTag>

    解决方法: <el-select v-model="syncParams.toSlaveList" multiple value-key="ip" pla ...

  7. PHP中的验证码类(验证码功能设计之一)

    <!--vcode.class.php内容--> <?php class Vcode { private $width; //宽 private $height; //高 priva ...

  8. python 粘包问题及解决方法

    一粘包 TCP协议是面向对象的,面向流的,提高可靠性服务.使用了优化算法,Nagle算法.将多次间隔较少且数据量小的数据,合并成一个大的数据块,然后进行封包.这样接收端就很难分辨出来.TCP协议数据是 ...

  9. Codevs 数字三角形 问题合集

    1220 数字三角形 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或得 ...

  10. 【CF707B】Bakery(想法题)

    题意: 有N个城市,M条无向边,其中有K个城市是仓库 现在要在非仓库的城市中选择一家开面包店,使得其最少与一个仓库联通,且到所有仓库距离的最小值最小 (1 ≤ n, m ≤ 10^5, 0 ≤ k ≤ ...