la4080 Warfare And Logistics 罗列+最短
为了图。计算最短随机分ans1。和删除边缘。免费才能够获得最大和短路之间的最大分ans2,如果这两个不沟通。看作是两个点之间的最短距离l。
第一个想法是枚举每个边缘,然后运行n最短时间。但是,这种复杂性是1000*1000*100*log(100),太大了..事实上在固定起点,求出单元最短路的时候。同一时候能够求出单源最短路树,仅仅有删除的边在树上的时候。源点到任一点的最短路才会有变化,所以在每次跑单源最短路的时候,仅仅须要枚举树上的n-1条边就能够了。累加一下删除每一条边时,在当前源点的情况下。最短距离之和的添加量,最后枚举找一条添加量最大的边就能够了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const ll inf=(1LL<<61);
ll cost[2020];
int n,m;
ll l; struct HeapNode
{ ll d;
int u;
bool operator< (const HeapNode& rhs) const
{
return d>rhs.d;
}
HeapNode(){}
HeapNode(ll x,int y)
{
d=x;
u=y;
}
};
struct Edge
{
int u,v;
ll w;
bool ok;
Edge(){}
Edge(int x,int y,ll z)
{
u=x;
v=y;
w=z;
ok=true;
}
};
const int maxn=105;
struct Dij
{
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 x,int y,ll z)
{
edges.push_back((Edge(x,y,z)));
m=edges.size();
G[x].push_back(m-1);
}
void dijkstra(int s)
{
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 (!e.ok) continue; if (d[e.v]>d[u]+e.w)
{
d[e.v]=d[u]+e.w;
p[e.v]=G[u][i];
q.push(HeapNode(d[e.v],e.v));
}
}
}
}
int tp[maxn];
ll slove(int s)
{
ll res=0;
ll add=0;
ll tmp=0;
ll maxx=0;
for (int i=1; i<=n; i++)
{
if (d[i]<inf)res+=d[i];
else res+=l;
}
memcpy(tp,p,sizeof p);
for (int i=1;i<=n; i++)
{
if (tp[i]!=-1)
{
edges[tp[i]].ok=false;
edges[tp[i]^1].ok=false;
dijkstra(s);
tmp=0;
for (int j=1; j<=n; j++)
{
if (d[j]<inf) tmp+=d[j];
else tmp+=l;
}
cost[tp[i]]+=(tmp-res);
cost[tp[i]^1]+=(tmp-res);
edges[tp[i]].ok=true;
edges[tp[i]^1].ok=true;
}
}
return res;
}
}work; int main()
{
// freopen("in.txt","r",stdin);
while (~scanf("%d%d%lld",&n,&m,&l))
{
int x,y;
ll z;
work.init(n);
for (int i=1; i<=m; i++)
{
scanf("%d%d%lld",&x,&y,&z);
work.addedge(x,y,z);
work.addedge(y,x,z);
}
memset(cost,0,sizeof cost);
ll ans=0;
for (int i=1; i<=n; i++)
{
work.dijkstra(i);
ans+=work.slove(i);
}
int id=0;
ll maxx=0;
ll ans2=0;
for (int i=0; i<work.edges.size(); i++)
{
if (cost[i]>maxx)
{
id=i;
maxx=cost[i];
}
}
work.edges[id].ok=false;
work.edges[id^1].ok=false;
for (int i=1; i<=n; i++)
{
work.dijkstra(i);
for (int j=1; j<=n; j++)
if (work.d[j]<inf) ans2+=work.d[j];
else ans2+=l;
}
cout<<ans<<" "<<ans2<<endl; }
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
la4080 Warfare And Logistics 罗列+最短的更多相关文章
- UVA1416/LA4080 Warfare And Logistics
题目大意:有N个点,M条路,如果两条路不连通的话,就将这两条路的距离设置为L 现在要求你求出每两点之间的最短距离和 接着要求 求出炸断 给出的M条路中的一条路后,每两点之间的最短距离和的最大值(翻译来 ...
- 【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条无向边的图,每条边权都为正.令 ...
- UVALive 4080 Warfare And Logistics (最短路树)
很多的边会被删掉,需要排除一些干扰进行优化. 和UVA - 1279 Asteroid Rangers类似,本题最关键的地方在于,对于一个单源的最短路径来说,如果最短路树上的边没有改变的话,那么最短路 ...
- UVA1416 Warfare And Logistics
UVA1416 Warfare And Logistics 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36232 [ ...
- LA4080/UVa1416 Warfare And Logistics 最短路树
题目大意: 求图中两两点对最短距离之和 允许你删除一条边,让你最大化删除这个边之后的图中两两点对最短距离之和. 暴力:每次枚举删除哪条边,以每个点为源点做一次最短路,复杂度\(O(NM^2logN)\ ...
- UVA 4080 Warfare And Logistics 战争与物流 (最短路树,变形)
题意: 给一个无向图,n个点,m条边,可不连通,可重边,可多余边.两个问题,第一问:求任意点对之间最短距离之和.第二问:必须删除一条边,再求第一问,使得结果变得更大. 思路: 其实都是在求最短路的过程 ...
- uva 1416 Warfare And Logistics
题意: 给出一个无向图,定义这个无向图的花费是 其中path(i,j),是i到j的最短路. 去掉其中一条边之后,花费为c’,问c’ – c的最大值,输出c和c’. 思路: 枚举每条边,每次把这条边去掉 ...
- Warfare And Logistics UVA - 1416
题目链接:https://vjudge.net/problem/UVA-1416 题解: 这是一个最短路的好题,首先我们考虑如果暴力弗洛伊德,显然时间复杂度不对,如果做n次spfa好像复杂度也不对,所 ...
- UVA - 1416 Warfare And Logistics (最短路)
Description The army of United Nations launched a new wave of air strikes on terroristforces. The ob ...
随机推荐
- 初识OpenStack(1)
初识OpenStack(1) 首先 先来说说我与openstack的渊源吧.那是在上个月中旬.学张的一个朋友给我打电话说让一起来搞一个云平台,当时也不知道是什么.就非常高兴的答应下来了,到了周末,就过 ...
- WCF REST 基础教程
概述 Representational State Transfer(REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格. 因此REST是设计风格而不是标准,R ...
- windows7 通过WSUS服务器更新,报错,错误代码800b0001
链接 您好,根据分析您的日志,可以看到“WARNING: WU client failed Searching for update with error 0x800b0001”等关键信息, 故障原因 ...
- php修改SESSION的有效生存时间
如何修改SESSION的生存时间 我们来手动设置 Session 的生存期: <?phpsession_start(); // 保存一天 $lifeTime = 24 * 3600; setco ...
- [转载]Ocelot简易教程(五)之集成IdentityServer认证以及授权
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9807125.html 最近比较懒,所以隔了N天才来继续更新第五篇Ocelot简易教程,本篇教程会先简单介 ...
- Multi-core compute cache coherency with a release consistency memory ordering model
A method includes storing, with a first programmable processor, shared variable data to cache lines ...
- mysqldump 不需要密码
-p 参数比较特殊,正确语法是 -ppassword,即-p和密码中间不能有空格. 请教:数据库备份命令如果这样写mysqldump -u root -p dataname>/home/data ...
- ios开发事件处理之 :二:事件的产生与传递
1.事件是怎么样产生与传递的? 当发生一个触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中.(队列是先进先出,而栈是先进后出) UIApplication会从事件队列中 ...
- 使用oschina的gitserver
1.概要 事实上oschina的gitserver与github的几乎相同.只是既然是中国的gitserver,那么速度应该更快一些吧 2.注冊 链接https://git.oschina.net/, ...
- 【u016】无序字母对
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个 ...