题目: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. [武汉集训] Cliquers

    题意 设把\(n\)个不同元素分成若干个大小相等的集合的方案个数为\(res\),求\(m^{res}\)模\(10^9-401\)后的余数. (n,m不超过2*10^9) 分析 可以知道,所求答案为 ...

  2. IntelliJ的Scala配置

    打开IDE: file->New->Project->Maven->Next 名字随便命名,到后面可以改的: 存放代码项目的位置,名字还是随便命名,可以改的,但是路径要自定义好 ...

  3. System.Data.Entity.Infrastructure.DbUpdateException

    异常描述:   捕捉到 System.Data.Entity.Infrastructure.DbUpdateException  HResult=-2146233087  Message=无法更新 E ...

  4. 并发编程(九)—— Java 并发队列 BlockingQueue 实现之 LinkedBlockingQueue 源码分析

    LinkedBlockingQueue 在看源码之前,通过查询API发现对LinkedBlockingQueue特点的简单介绍: 1.LinkedBlockingQueue是一个由链表实现的有界队列阻 ...

  5. asp.net core 系列 18 web服务器实现

    一. ASP.NET Core Module 在介绍ASP.NET Core Web实现之前,先来了解下ASP.NET Core Module.该模块是插入 IIS 管道的本机 IIS 模块(本机是指 ...

  6. jxa快速入门,Javascript已加入AppleScript全家桶

    因为工作环境基本是以跨平台为主,所以纯mac本地化的AppleScript一直关注是不够的,前几天找资料发现AppleScript也在迅速的进步着,目前已经对Javascript做了比较好的支持--- ...

  7. ELK-Logstash采集日志和输送日志流程测试

    讲解Logstash采集日志和输送日志流程测试,包括input,filter和output元素的测试 配置一:从elasticsearch日志文件读取日志信息,输送到控制台 $ cd /home/es ...

  8. git版本控制工具的使用

    目录 git版本管理工具使用 一丶Git的下载与安装 1.windows下的git的下载与安装 2.linux下的git安装 二丶常用命令 三丶Git仓库 1.配置仓库信息 2.仓库的创建于管理 四丶 ...

  9. Java提高班(四)面试必备—你不知道的数据集合

    导读:Map竟然不属于Java集合框架的子集?队列也和List一样属于集合的三大子集之一?更有队列的正确使用姿势,一起来看吧! Java中的集合通常指的是Collection下的三个集合框架List. ...

  10. 痞子衡嵌入式:串口调试工具Jays-PyCOM诞生记(3)- 串口功能实现(pySerial)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是串口调试工具Jays-PyCOM诞生之串口功能实现. 串口调试助手是最核心的当然是串口数据收发与显示的功能,Jays-PyCOM借助的是 ...