题意:

给出一个无向图,定义这个无向图的花费是

其中path(i,j),是i到j的最短路。

去掉其中一条边之后,花费为c’,问c’ – c的最大值,输出c和c’。

思路:

枚举每条边,每次把这条边去掉,然后以每个点为起点,跑一次Dijkstra,再计算总花费。

这样的复杂度为O(mn^2log(n)),这个复杂度刚好卡着时间过,所以是暴力,更优的方法是最短路树(待学习。

代码:

 #include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std; struct edge
{
int to,cost;
int id; edge(int uu,int vv,int idd)
{
to = uu;
cost = vv;
id = idd;
}
}; const int maxn = ; typedef pair<long long,int> pii;
long long d[maxn];
vector<edge> g[maxn];
bool ma[]; void adde(int st,int en,int cost,int id)
{
g[st].push_back(edge(en,cost,id));
} void dij(int s,int n)
{
for (int i = ;i <= n;i++) d[i] = 1e16; priority_queue<pii,vector<pii>,greater<pii> > pq; pq.push(pii(,s)); d[s] = ; //printf("***\n"); while (!pq.empty())
{
pii x = pq.top();pq.pop(); int v = x.second; if (d[v] < x.first) continue; for (int i = ;i < g[v].size();i++)
{
edge e = g[v][i]; if (d[e.to] > d[v] + e.cost)
{
d[e.to] = d[v] + e.cost;
pq.push(pii(d[e.to],e.to));
}
}
} //printf("***\n");
} void dijk(int s,int n)
{
for (int i = ;i <= n;i++) d[i] = 1e16; priority_queue<pii,vector<pii>,greater<pii> > pq; pq.push(pii(,s)); d[s] = ; while (!pq.empty())
{
pii x = pq.top();pq.pop(); int v = x.second; if (d[v] < x.first) continue; for (int i = ;i < g[v].size();i++)
{
edge e = g[v][i]; if (ma[e.id]) continue; if (d[e.to] > d[v] + e.cost)
{
d[e.to] = d[v] + e.cost;
pq.push(pii(d[e.to],e.to));
}
}
} //printf("***\n");
} int main()
{
int n,m,l; while (scanf("%d%d%d",&n,&m,&l) != EOF)
{
for (int i = ;i <= n;i++) g[i].clear(); for (int i = ;i < m;i++)
{
int a,b,c; scanf("%d%d%d",&a,&b,&c); adde(a,b,c,i);
adde(b,a,c,i);
} long long ans = ; for (int i = ;i <= n;i++)
{ dij(i,n); for (int j = ;j <= n;j++)
{
if (d[j] == 1e16) d[j] = l;
ans += d[j];
}
} long long res = ;
long long tmp = ; for (int i = ;i < m;i++)
{
ma[i] = ; long long orz = ; for (int j = ;j <= n;j++)
{
dijk(j,n); for (int k = ;k <= n;k++)
{
if (d[k] == 1e16) d[k] = l;
orz += d[k];
}
} if (orz - ans > tmp)
{
res = orz;
tmp = orz - ans;
} ma[i] = ;
} printf("%lld %lld\n",ans,res);
} return ;
}

uva 1416 Warfare And Logistics的更多相关文章

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

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

  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. UVALive 4080 Warfare And Logistics (最短路树)

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

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

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

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

  8. la4080 Warfare And Logistics 罗列+最短

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

  9. UVA1416/LA4080 Warfare And Logistics

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

随机推荐

  1. 基于Gogs+Drone搭建的私有CI/CD平台

    请移步 基于Gogs+Drone搭建的私有CI/CD平台

  2. 关于话题模型(topic model)的一些思考

    最近在分析知乎的‘问题’文本所属的话题,用python提取,实现了LSTM和LDA模型在这个方面的应用,但是效果不是很理想,一个是这些文本属于短文本,另外用来分析的文本本身包含多个领域的问题,并且数量 ...

  3. 如何监控 Java 垃圾回收机制: jps、jstack、jmap、jhat、jstat

    一.MinorGC 一个新对象会被放到eden空间,当eden空间满了的时候,MinorGC就会执行,任何存活的对象,都从eden空间复制到to survivor空间,任何在from survivor ...

  4. 如何调用finecms指定栏目的描述关键词

    有时我们在用finecms建站时需要调用指定栏目的描述和关键词,实现个性化需求,比如id为23的栏目很重要,要让它在首页展示出来,这时我们要如何调用呢?{dr_cat_value(23, 'name' ...

  5. LeetCode之有效括号

    class Solution:    def isValid(self,s):         #第一步,if 判断传入为空的情况        if s == "":      ...

  6. (转)区块链共识机制分析——论PoW,PoS,DPos和DAG的优缺点

    近期,随着区块链技术在社区中的声音越来越大,业界已经开始从技术角度对区块链进行全方位的解读.作为第一批区块链技术的实现,传统比特币与以太坊在共识机制.存储机制.智能合约机制.跨链通讯机制等领域并没有非 ...

  7. 安装ElasticSearch5.5.2 注意事项

    官方文档中建议生产环境中打开 bootstrap.memory_lock: true 打开之后会报很多错误要优化一下系统参数 vim /etc/security/limits.conf * soft ...

  8. 30-Python3 正则表达式

    30-Python3 正则表达式 ''' re.match函数 ''' import re print(re.match('www','www.runoob.com').span()) print(r ...

  9. [LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  10. jQuery发布1.9正式版,最后支持IE 6/7/8

    jQuery 于 2013/1/15 正式发布了 1.9 版本,这个版本最值得关注的,不是又增加了什么新功能,而是它去掉了哪些东西!jQuery 1.9 删除和改动了不少过时的 API,升级后可能会导 ...