题目大意:有N个点,M条路,如果两条路不连通的话,就将这两条路的距离设置为L
现在要求你求出每两点之间的最短距离和
接着要求
求出炸断 给出的M条路中的一条路后,每两点之间的最短距离和的最大值(翻译来自http://blog.csdn.net/l123012013048/article/details/47297393)

单源最短路树:把源点到其他点的最短路拼起来,形成最短路树(可能有多棵,这里只需要一棵)。我们把起点为i的单源最短路树称为i源最短路树。想要让最短路改变,删除的边必定在这课最短路树上(但反过来不成立,即删除最短路树上的边最短路改变,是错误的)。处理出单源最短路树,然后用belong[i][j]表示边i是否在j源最短路树上,枚举边,重新做belong[i][j]为1的点即可。

这里仅仅是带来常数上的优化,实际复杂度并不能改变(nm^2logn),蓝书说复杂度变了(n^2mlogn)。。我不敢苟同

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
template<class T>
inline void swap(T &a, T &b)
{
T tmp = a;a = b;b = tmp;
}
inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '') c = ch, ch = getchar();
while(ch <= '' && ch >= '') x = x * + ch - '', ch = getchar();
if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
const int MAXN = + ;
const int MAXM = + ;
struct Edge
{
int u,v,w,nxt;
Edge(int _u, int _v, int _w, int _nxt){u = _u, v = _v, w = _w, nxt = _nxt;}
Edge(){}
}edge[MAXM << ];
int head[MAXN], cnt = , d[MAXN][MAXN], dis[MAXM], flag, belong[MAXM][MAXN], vis[MAXN], p[MAXN][MAXN], l, tmp1, tmp2, tmp3, n, m;
//p[i]表示以i为起点的最短路树,在最短路树上,j的父亲的那条边
inline void insert(int a, int b, int c)
{
edge[++ cnt] = Edge(a,b,c,head[a]), head[a] = cnt;
}
struct Node
{
int u,w;
Node(int _u, int _w){u = _u, w = _w;}
Node(){}
};
struct cmp
{
bool operator()(Node a, Node b)
{
return a.w > b.w;
}
};
std::priority_queue<Node, std::vector<Node>, cmp> q;
void dij(int S, int *d, int size)
{
while(q.size())q.pop();memset(d, 0x3f, size), d[S] = , memset(vis, , sizeof(vis)), q.push(Node(S, ));
while(q.size())
{
Node now = q.top();q.pop();
if(vis[now.u]) continue; vis[now.u] = ;
for(int pos = head[now.u];pos;pos = edge[pos].nxt)
{
int v = edge[pos].v;
if(d[v] > d[now.u] + edge[pos].w)
{
d[v] = d[now.u] + edge[pos].w, q.push(Node(v, d[v]));
if(flag) p[S][v] = pos;
}
}
}
}
int main()
{
while(scanf("%d %d %d", &n, &m, &l) != EOF)
{
flag = , memset(head, , sizeof(head)), memset(belong, , sizeof(belong)), memset(p, , sizeof(p)), cnt = ;
for(int i = ;i <= m;++ i) read(tmp1), read(tmp2), read(tmp3), insert(tmp1, tmp2, tmp3), insert(tmp2, tmp1, tmp3);
for(int i = ;i <= n;++ i) dij(i, d[i], sizeof(d[i]));
long long ans1 = , ans2 = ans1, ans3 = ;
for(int i = ;i <= n;++ i)
for(int j = ;j <= n;++ j)
if(d[i][j] >= INF) ans1 += l;
else ans1 += d[i][j];
printf("%lld ", ans1);
flag = ans3 = ;
for(int i = ;i <= n;++ i)
for(int j = ;j <= n;++ j)
if(p[i][j]) belong[p[i][j]][i] = ;
for(int pos = ;pos <= cnt;pos += )
{
tmp1 = edge[pos].w;ans2 = ans1;
edge[pos].w = edge[pos ^ ].w = INF;
for(int S = ;S <= n;++ S)
if(belong[pos][S] || belong[pos^][S])
{
dij(S, dis, sizeof(dis));
for(int i = ;i <= n;++ i)
{
if(d[S][i] >= INF) ans2 -= l;
else ans2 -= d[S][i];
if(dis[i] >= INF) ans2 += l;
else ans2 += dis[i];
}
}
edge[pos].w = edge[pos ^ ].w = tmp1;
ans3 = max(ans3, ans2);
}
printf("%lld\n", ans3);
}
return ;
}

UVA1416/LA4080

UVA1416/LA4080 Warfare And Logistics的更多相关文章

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

  2. la4080 Warfare And Logistics 罗列+最短

    为了图.计算最短随机分ans1.和删除边缘.免费才能够获得最大和短路之间的最大分ans2,如果这两个不沟通.看作是两个点之间的最短距离l. 第一个想法是枚举每个边缘,然后运行n最短时间.但是,这种复杂 ...

  3. UVA1416 Warfare And Logistics

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

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

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

  5. Warfare And Logistics UVA - 1416

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

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

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

  7. uva 1416 Warfare And Logistics

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

  8. UVA - 1416 Warfare And Logistics (最短路)

    Description The army of United Nations launched a new wave of air strikes on terroristforces. The ob ...

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

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

随机推荐

  1. 2019 Multi-University Training Contest 6 Snowy Smile (最大字段和变形)

    题意: 求一个子矩阵要求其矩阵内的合最大. 题解: 正常的求最大子矩阵的复杂度是O(n^3) 对于这一题说复杂度过不去,注意到这个题总共只有2000个点关键点在与这里优化 最大子矩阵可以压缩矩阵变成最 ...

  2. 自动生成DTO(Sugar框架)

    step1:启动api项目 step2:使用postman工具,填上接口地址http://localhost:7788/api/automapper/AutoMapperSuper step3:表格数 ...

  3. LeetCode 14.最长公共前缀(Python3)

    题目: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow& ...

  4. Android开发 EditText按回车按键后出现 focus search returned a view that wasn't able to take focus! 错误

    问题描述 将EditText这个View成为了ListView或者RecyclerView的item时,在按输入法的回车/下一步/next时会出现的 focus search returned a v ...

  5. css3 ---2 属性的选择器

    存在和值属性选择器1:[attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何. [name]{ background: pink; } <!DOCTYPE html& ...

  6. python的meshgrid用法和3D库 mpl_toolkits.mplot3d 与PolynomialFeatures多项式库学习

    meshgrid import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Ax ...

  7. 线段树分治初步学习&洛谷P5227[AHOI2013]连通图

    线段树分治 其实思想说起来是比较简单的,我们把这个题里的所有操作(比如连边删边查询balabala)全部拍到一棵线段树上,然后对着整棵树dfs一下求解答案,顺便把操作做一下,回溯的时候撤销一下即可.虽 ...

  8. 07_jQuery对象初识(五)事件(非常重要)

    1. 目前为止学过的绑定事件的方式 1. 在标签里面写 onclick=foo(this); 2. 原生DOM的JS绑定 DOM对象.onclick=function(){...} 3. jQuery ...

  9. .Net Core微服务系列--服务发现

    什么是服务发现 首先我们先思考一个问题,当我们在浏览器中输入一个域名比如baidu.com,然后发生了什么才能让我们访问到百度的网页?简单来说,浏览器会首先从主机的hosts文件中查看是否有baidu ...

  10. 网络结构解读之inception系列二:GoogLeNet(Inception V1)

    网络结构解读之inception系列二:GoogLeNet(Inception V1) inception系列的开山之作,有网络结构设计的初期思考. Going deeper with convolu ...