题意:

  给一个无向图,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. Vim 配置 winmanager

    问题描述: winmanager是vim中插件,可以方便的查看当前文件夹中文件,可以切换vim打开文件,非常方便 现在说明安装和使用winmanager 问题解决: (1)winmanager源文件 ...

  2. 剑指offer--面试题8

    题目:求旋转数组中的最小数字 以下为自己所写代码: #include "stdafx.h" #include <iostream> #include <excep ...

  3. oracle 条件:1=1或1=0,动态添加条件

    看到where语句中有条件:where 1=1    和    1=2或1<>1 用途:     1=1:是为了添加条件时使用and并列其他条件时使用的(动态连接后续条件)     比如: ...

  4. Bootstrap 基础

    一种前端开发框架,如同YUI 下载源码找开后,其文件结构如下: bootstrap/├── css/│   ├── bootstrap.css│   ├── bootstrap.min.css│   ...

  5. CKFinder 1.4.3 任意文件上传漏洞

    CKFinder 是国外一款非常流行的所见即所得文字编辑器,其1.4.3 asp.net版本存在任意文件上传漏洞,攻击者可以利用该漏洞上传任意文件. CKFinder在上传文件的时候,强制将文件名(不 ...

  6. Namespace, string, vector and array

    1. Headers should not include using declaration Code inside headers ordinarily should not include us ...

  7. linux下获取时间差

    #include <sys/time.h> struct timeval tpstart,tpend;     float timeuse;     gettimeofday(&t ...

  8. struts2 json关于Date日期的解析

    在get方法前加上: @JSON(format="yyyy-MM-dd HH:mm:ss")

  9. Mysql 1030 Got error -1 from storage engine 错误解决

    检查你的my.cnf或my.ini,里面会有一个参数innodb_force_recovery,你看看他的值,默认是没有这个参数,没有的话,他的默认值是0,这个参数的值如果大于0,innodb会被禁止 ...

  10. 深入理解JVM—字节码执行引擎

    原文地址:http://yhjhappy234.blog.163.com/blog/static/3163283220122204355694/ 前面我们不止一次的提到,Java是一种跨平台的语言,为 ...