题目:Bear and Clique Distances

描述:共有N个点,前1—K个点任意两点之间有一条无向边,边的权值为X,再任意给M条边(u,v,w)(不重复),求任意一点到其余各点的最短路。

分析:

1.最短路算法(usual Dijkstra)+一点小变形(a little twist)

2.trick:设置一个变量upd=1;因为前K个点之前两两是连通的,所以当第一次到前K个点中任一点(假设为u)时,就可以更新得到前K个点的距离(除了u)d[i](i<=k)为d[u]+X。

即:1-K中任意一点作为中间点更新1-K之间其他点的距离只用更新一次,因为最短路算法中每次队列取出来的点都是当前剩下点中距离源点最近的点,还有就是下次再更新到1-K点中的点时不用再继续更新与该点相连的1-K点的距离了

3.复杂度:O((m+k)log(V))

4.边的权值范围到达1e9,前面WA了几次,因为初始化inf=1e9+10;值太小了,有很多边,假如所有边大小都是1e9,则路径长度加起来远远大于初始化位1e9+10的inf。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map> using namespace std;
#define maxn 100000+10
#define inf 1e15+10 //该题中inf定义为1e9+10 太小,导致出错
#define ll long long struct Edge{
ll u,v,w;
Edge(ll u=,ll v=,ll w=):u(u),v(v),w(w){}
}; struct Node{
ll d,u;
bool operator<(const Node& rhs)const{
return d>rhs.d;
}
}; Edge edges[maxn];
vector<ll>G[maxn];
ll N,M,X,K,S;
bool vis[maxn];
ll d[maxn]; void Dijkstra()
{
bool flag=false;
priority_queue<Node>Q; for(int i=;i<=N;i++) d[i]=inf;
//memset(d,127,sizeof(d));
memset(vis,,sizeof(vis)); d[S]=;
Q.push(Node{,S});
while(!Q.empty())
{
Node x=Q.top();Q.pop();
//cout<<x.d<<" "<<x.u<<endl;
ll u=x.u;
if(vis[u]) continue;
vis[u]=true;
if(u<=K && !flag)
{
flag=true;
for(int i=;i<=K;i++)
{
if(i!=u && d[i]>d[u]+X)
{
d[i]=d[u]+X;
Q.push(Node{d[i],i});
}
}
}
//cout<<G[u].size()<<endl;
for(int i=;i<G[u].size();i++)
{
ll id=G[u][i];
//cout<<id<<endl;
ll x1=edges[id].u,x2=edges[id].v,x3=edges[id].w;
ll v=u==x1?x2:x1;
if(d[v]>d[u]+x3)
{
d[v]=d[u]+x3;
Q.push(Node{d[v],v});
}
} } } int main()
{
int t;
scanf("%d",&t);
while(t--)
{ scanf("%lld%lld%lld%lld%lld",&N,&K,&X,&M,&S);
for(int i=;i<=N;i++) G[i].clear();
for(int i=;i<M;i++)
{
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
edges[i]=Edge(a,b,c);
G[a].push_back(i);
G[b].push_back(i);
}
//for(int i=1;i<=N;i++) cout<<G[i].size()<<" ";
//cout<<endl;
Dijkstra(); for(int i=;i<=N;i++)
printf("%lld%c",d[i],i==N?'\n':' ');
}
return ;
}

Codechef Bear and Clique Distances的更多相关文章

  1. CodeChef Sum of distances(分治)

    CodeChef Sum of distances(分治) 题目大意 有一排点,每个点 i 向 \(i + 1, i + 2, i + 3\) 分别连价值为 \(a_i,b_i,c_i\) 的有向边, ...

  2. Codeforces CF#628 Education 8 C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. Educational Codeforces Round 8 C. Bear and String Distance 贪心

    C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...

  4. codeforces 628C C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. codechef营养题 第三弹

    第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Rac ...

  6. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

  7. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. UVA11324 The Largest Clique[强连通分量 缩点 DP]

    UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...

  9. CF #296 (Div. 1) B. Clique Problem 贪心(构造)

    B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. 是程序员,就用python导出pdf

    这两天一直在做课件,我个人一直不太喜欢PPT这个东西--能不用就不用,我个人特别崇尚极简风. 谁让我们是程序员呢,所以就爱上了Jupyter写课件,讲道理markdown也是个非常不错的写书格式啊. ...

  2. 46道史上最全Redis面试题,面试官能问的都被我找到了(含答案)

    Redis高性能缓存数据库 1.什么是 Redis?简述它的优缺点? Redis 的全称是:Remote Dictionary.Server,本质上是一个 Key-Value 类型的内存数据库,很像m ...

  3. Elasticsearch 分词器

    无论是内置的分析器(analyzer),还是自定义的分析器(analyzer),都由三种构件块组成的:character filters , tokenizers , token filters. 内 ...

  4. Object类toString()

    Object类是java所有类的始祖,在java中每个类都是由它扩展而来. toString()方法返回一个包含类名与内存地址的文本形式的字符串  即打印对象的时候便会调用此方法. 强烈建议为自定义的 ...

  5. mysql更改数据存储目录

    具体操作请参考文章 1.http://blog.csdn.net/aaronbai/article/details/1431190 更改数据存储目录时候会出现 ERROR 2002 (HY000): ...

  6. Linux vi常用命令

    vi常用命令[Ctrl] + [f] 屏幕『向前』移动一页(常用)[Ctrl] + [b] 屏幕『向后』移动一页(常用)0 这是数字『 0 』:移动到这一行的最前面字符处(常用)$ 移动到这一行的最后 ...

  7. 为容器化的 Go 程序搭建 CI

    本文介绍如何使用 Jenkins 的声明式 pipeline 为一个简单的 Golang web 应用搭建 CI 环境.如果你还不太了解 Jenkins 及其声明式 pipeline,请先参考笔者的 ...

  8. Smobiler 4.4已正式发布!(Smobiler能让你在Visual Studio上开发APP)

    Smobiler 4.4已经正式发布,还不快来看看?原文地址:https://www.smobiler.com/portal.php?mod=view&aid=53这次更新要感谢我们的用户,在 ...

  9. 强大的数据库工具 dbForge Studio ForMySql

    优点: 1.可以将MySql数据库操作仿 sqlserver 的操作方式,便于操作 2.强大的比较拷贝能力.菜单栏上的 Comparison 的功能,可以比较两个数据库的差别,同时可以将数据库Copy ...

  10. 重写(override)和重载(overload)的区别

    override(重写): 是进行基类中函数的重写,是面向对象的概念 重载(overload):是方法的名称相同,参数或参数类型不同,进行多次重载以适应不同的需要.overload 是面向对象的概念.