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 ...
随机推荐
- C语言深度剖析-----函数与指针的分析
指针的本质 指针需要保证指向任意数据类型,所以指针变量都占用32位bit即4字节. PS:不同机器上,指针占用内存不一 ...
- POJ3984 迷宫问题 BFS
看题传送门:http://poj.org/problem?id=3984 BFS水一发 明天帮学弟挑电脑顺便去玩.接下来几天好好看数据结构.嗯哼. 这题标准的BFS应用,唯一需要注意的是需要输出中间的 ...
- 【BZOJ 4516】生成魔咒
[链接]h在这里写链接 [题意] [Description] 给你n(n<=10^9)个数字,把它们依次,一个一个地添加在空串S的后面. 要求每添加一次之 ...
- [CSS3] Create a fixed-fluid-fixed layout using CSS calc()
CSS calc() allows you to mix and match units to get real-time calculations. It's useful when you nee ...
- 18、IIC总线驱动程序
i2c_s3c2410.c是内核自带dev层(adapt)驱动程序,知道怎么发收数据,不知道含义 在与i2c_s3c2410.c(在其probe函数中的s3c24xx_i2c_init函数会初始化ii ...
- [RxJS] Replace zip with combineLatest when combining sources of data
This lesson will highlight the true purpose of the zip operator, and how uncommon its use cases are. ...
- [RxJS] Reusable multicasting with Subject factories
The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusabl ...
- 在Eclipse中运行Nutch2.3 分类: H3_NUTCH 2015-01-28 16:41 3175人阅读 评论(13) 收藏
参考http://wiki.apache.org/nutch/RunNutchInEclipse 一.环境准备 1.下载nutch2.3源代码 wget http://mirror.bit.edu.c ...
- js进阶 11-16 jquery如何查找元素的父亲、祖先和子代、后代
js进阶 11-16 jquery如何查找元素的父亲.祖先和子代.后代 一.总结 一句话总结:过滤或者查找的方法里面可以带参数进行进一步的选择. 1.parent()和parents()方法的区别是什 ...
- web报表工具FineReport经常使用函数的使用方法总结(文本函数)
文本函数 CHAR CHAR(number):依据指定数字返回相应的字符.CHAR函数可将计算机其它类型的数字代码转换为字符. Number:用于指定字符的数字,介于1Number:用于指定字符的数字 ...