题意:

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

思路:

  其实都是在求最短路的过程。

  第一问可以floyd解决,也可以SSSP解决。注意是任意两个点,(a,b)和(b,a)是不同的,都要算。

  第二问要穷举删除每条边,再求第一问。为了降低复杂度,假设用dijkstra求最短路,那么可以利用第一问中所生成的树,共n棵,每棵至多n-1条边,如果穷举的边不在该某树上,那么该树的所有路径长不变,不必计算,否则需要计算。所以需要记录路径,并将整棵树的边集存起来,同时保存每棵树的任意两点路径之和。

  用结构体可以解决重边,问题应该不多,要注意各种细节,错了就重新打,也许更快。

  

 #include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=;
int n, m, l, edge_cnt;
vector<int> vect[N]; struct node
{
int from, to, dis,tag;
node(){};
node(int from,int to,int dis,int tag):from(from),to(to),dis(dis),tag(tag){};
}edge[]; void add_node(int from,int to,int dis,int tag)
{
edge[edge_cnt]=node(from, to, dis, tag);
vect[from].push_back(edge_cnt++);
} int dist[N], vis[N], path[N];
LL dijkstra(int s)
{
memset(dist,0x7f,sizeof(dist));
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++) path[i]=-; priority_queue<pii,vector<pii>,greater<pii> > que;
dist[s]=;
que.push(make_pair(,s)); while(!que.empty())
{
int x=que.top().second;que.pop();
if(vis[x]) continue;
vis[x]=;
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(e.tag> && dist[e.to]>dist[e.from]+e.dis )
{
path[e.to]=vect[x][i];
dist[e.to]=dist[e.from]+e.dis;
que.push(make_pair(dist[e.to], e.to));
}
}
} LL sum=;
for(int i=; i<=n; i++ )
{
if(dist[i]>=INF) sum+=l;//不可达的,按L算
else sum+=dist[i];
}
return sum;
} LL ans1[N];
int cal()
{
memset(ans1,,sizeof(ans1));
LL first=;
unordered_set<int> tree[N];
for(int i=; i<=n; i++)
{
ans1[i]=dijkstra(i);
first+=ans1[i];
//收集边
for(int k=; k<=n; k++)
{
if(path[k]>=)//注意如何初始化
{
tree[i].insert(path[k]);
tree[i].insert(path[k]^);
}
}
}
//另一个问
LL second=;
for(int i=; i<edge_cnt; i+=)
{
edge[i].tag=edge[i+].tag=;
LL sum=;
for(int j=; j<=n; j++)
{
if( tree[j].find(i)==tree[j].end() ) //是点j的树上,要重新算
sum+=ans1[j];
else
sum+=dijkstra(j);
}
second=max(second, sum);
edge[i].tag=edge[i+].tag=;
}
printf("%lld %lld\n", first, second );//仅1个空格
} int main()
{
freopen("input.txt", "r", stdin);
int a, b, c;
while(scanf("%d%d%d", &n, &m, &l)==)
{
edge_cnt=;
memset(edge,,sizeof(edge));
for(int i=; i<=n; i++) vect[i].clear();
for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
if(a==b) continue;
add_node(a,b,c,);
add_node(b,a,c,);
}
cal();
}
return ;
}

AC代码

UVA 4080 Warfare And Logistics 战争与物流 (最短路树,变形)的更多相关文章

  1. 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)

    layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...

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

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

  3. uva 1416 Warfare And Logistics

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

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

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

  5. UVA1416 Warfare And Logistics

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

  6. LA 4080 战争和物流(最短路树)

    https://vjudge.net/problem/UVALive-4080 题意:给出一个n个结点m条边的无向图,每条边上有一个正权.令c等于每对结点的最短路长度之和.不连通的两点的最短路长度视为 ...

  7. Warfare And Logistics UVA - 1416

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

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

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

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

随机推荐

  1. 'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件的解决办法

    前言: 本文的解决方法来自http://www.cnblogs.com/xionghui/archive/2012/04/11/2442404.html --感谢! 问题描述:新电脑装mysql后在c ...

  2. 平常写css网页制作时最实用的九条CSS技巧

    一.使用css缩写 使用缩写可以帮助减少你CSS文件的大小,更加容易阅读.css缩写的主要规则请参看<css基本语法>. 二.明确定义单位,除非值为0 忘记定义尺寸的单位是CSS新手普遍的 ...

  3. [转载]如何申请淘宝app_key、app_secret、SessionKey?

    不知道如何申请淘宝开发平台的App Key?其实申请App key很简单,主要了解申请步骤以及各个App key的数据阶段状态就可以了!下面由淘客帝国为您做详细图文讲解!申请比较简单,不过为了新手能够 ...

  4. Error: Exception in thread “main” java.lang.NoClassDefFoundError错误

    Error: Exception in thread “main” java.lang.NoClassDefFoundError错误 检查文件名与类名是否一致 检查程序中main方法写的是否正确: p ...

  5. What is the difference between database table and database view?

    The database table has a physical existence in the database. A view is a virtual table, that is one ...

  6. Class

    1. No const constructor Unlike other member functions, constructors may not be declared as const . W ...

  7. SQLMap使用

    http://www.freebuf.com/articles/web/29942.html http://sqlmap.org/ http://blog.csdn.net/zgyulongfei/a ...

  8. ZOJ 3791 An Easy Game(DP)

    题目链接 题意 : 给你两个长度为N的字符串,将第一个字符串每次只能变化M个,问变换K次之后变成第二个字符串一共有几种方法. 思路 : DP.dp[i][j]表示变了 i 次之后有j个不一样的字母的方 ...

  9. REST_FRAMEWORK加深记忆-加了用户登陆认证,自定义权限的API接口

    哈哈,终于快结束了.. urls.py from django.conf.urls import include, url from django.contrib import admin urlpa ...

  10. poj 3604 Professor Ben

    质因数分解:牛人推导公式(1^3+2^3+……+(1+a1)^3)*……*(1^3+2^3+……+(1+ai)^3)…… 链接http://poj.org/problem?id=3604 #inclu ...