题目: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. 【译】编写支持SSR的通用组件指南

    原文来自:https://blog.lichter.io/posts/the-guide-to-write-universal-ssr-ready-vue-compon?utm_campaign=Vu ...

  2. Java String:重要到别人只能当老二的字符串类

    字符串,是Java中最重要的类.这句肯定的推断不是Java之父詹姆斯·高斯林说的,而是沉默王二说的,因此你不必怀疑它的准确性. 关于字符串,有很多的面试题,但我总觉得理论知识绕来绕去没多大意思.你比如 ...

  3. Python使用Xpath轻松爬虫(脑残式)

    1.在PyCharm安装lxml. 2.找到源码 3.F12.copy源码的xpath 4.代码 from lxml import etree import requests wb_data = re ...

  4. linux-centerOs6.8安装nginx与配置

    一:安装nginx 1.安装gcc(命令:yum install gcc)备注:可以输入gcc -v查询版本信息,查看是否自带安装 2.安装pcre(命令:yum install pcre-devel ...

  5. sql server 临时表(中) Tempdb监控

     一. 监控概述 Tempdb库空间使用的一大特点,是只有一部分对象,例如用户创建的临时表.table变量等,可以用sys.allocation_units和sys.partitions这样的管理视图 ...

  6. C#版 - HDUoj 5391 - Zball in Tina Town(素数) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. HDUoj 5 ...

  7. Vue依赖收集引发的问题

    问题背景 在我们的项目中有一个可视化配置的模块,是通过go.js生成canvas来实现的.但是,我们发现这个模块在浏览器中经常会引起该tab页崩溃.开启chrome的任务管理器一看,进入该页面内存和c ...

  8. 【性能优化之道】每秒上万并发下的Spring Cloud参数优化实战

    一.写在前面   相信不少朋友都在自己公司使用Spring Cloud框架来构建微服务架构,毕竟现在这是非常火的一门技术. 如果只是用户量很少的传统IT系统,使用Spring Cloud可能还暴露不出 ...

  9. Java开发知识之Java的集成开发环境

    Java开发知识之Java的集成开发环境 一丶Eclipse 开发环境 Eclipse是IBM公司花了4000万美金开发的一个集成开发环境.是一个免费开源的. 下载官网: http://www.ecl ...

  10. Windows 下常见的反调试方法

    稍稍总结一下在Crack或Rervese中比较常见的一些反调试方法,实现起来也比较简单,之后有写的Demo源码参考,没有太大的难度. ①最简单也是最基础的,Windows提供的API接口:IsDebu ...