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. 基于JQuery的WEB套打设计器jatoolsPrinter1.0

    开发web套打应用时,如快递单打印,一般要经过以下步骤:1. 扫描快递单据,保存成一个图片文件2. 将底图作成<img>3. 在<img>上放置打印项,试着打印到打印机,观察有 ...

  2. 编译参数-ObjC的说明

    一些第三方库里对系统库的类加了 category , 这时,就需要使用编译参数: -ObjC ,这样第三方库中对系统类作的扩展方法才能在工程中使用. 但是使用 -Objc 后,会产生两个问题: 1 . ...

  3. python学习-- 数据库迁移 python manage.py makemigrations 和 python manage.py migrate

    python manage.py makemigrations 和 python manage.py migrate

  4. Leetcode 479.最大回文数乘积

    最大回文数乘积 你需要找到由两个 n 位数的乘积组成的最大回文数. 由于结果会很大,你只需返回最大回文数 mod 1337得到的结果. 示例: 输入: 2 输出: 987 解释: 99 x 91 = ...

  5. JAVA-json数据与Java的bean类的互相转换

    Java调用webservice时用到了json格式的数据,然后就整理这个类.那里不合适的话,希望能够得到指正. public class JsonsAndBeanList { /** * json数 ...

  6. Python循环语句 if while for

    流程控制: if 条件1: 缩进的代码块 (注意缩进4个空格) elif 条件2: 缩进的代码块 elif 条件3: 缩进的代码块 ...... else: 缩进的代码块 注意1:(相同的代码块儿,同 ...

  7. NuGet之控制台管理程序包

        NuGet作为VS的扩展程序,已经做好了UI,我们可以通过Manage NuGet Packages 的对话框.这里我们主要说说如何通过控制台进行包管理.使用命令行的方式,其实也是有其好处,对 ...

  8. 【bzoj2400】Spoj 839 Optimal Marks 网络流最小割

    题目描述 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有n个结点m条边的无向图.其中的一些点的值是给定的,而其余的点的值由你 ...

  9. iOS-ASIHTTPRequest缓存机制

    第三方网络请求库 * 我们在对网络请求的时候,可以使用系统为我们提供的NSURLRequest和NSURLConnection,它基本能实现我们的基本功能. * 但是有时我们使用第三方封装的库,可以轻 ...

  10. docker 查询或获取私有仓库(registry)中的镜像

    docker 查询或获取私有仓库(registry)中的镜像,使用 docker search 192.168.1.8:5000 命令经测试不好使. 解决: 1.获取仓库类的镜像: [root@sha ...