题目: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. 基于ipv6的数据抓包

    一.实验拓扑 二.配置过程 以r1为例 R1: R1(config)#int f0/0 R1(config-if)#ipv6 enable R1(config-if)#ipv6 address 200 ...

  2. BBS论坛(一)

    1.1.项目结构搭建 (1)创建flask项目Perfect_bbs,然后搭建项目结构如下: (2)构建蓝图 cms/views.py # cmd/views.py from flask import ...

  3. HTML基础知识点

    HTML    1.一套规则,浏览器认识的规则.    2.开发者:        学习Html规则        开发后台程序:            - 写Html文件(充当模板的作用) **** ...

  4. linux-php5.6-安装sftp扩展

    一. 更新gc库,添加libssh2库 yum -y install libstdc libgomp cpp gcc libgfortran libssh2 libssh2-devel gcc-gfo ...

  5. ASP.NET Core 四种方式绑定枚举值

    前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...

  6. PHP中Smarty引擎的常用语法

    PHP中Smarty引擎的常用语法 输出今天的日期: {$smarty.now|date_format:"%H:%M %A, %B %e, %Y"} 实际上用到了PHP的time( ...

  7. 知其所以然~mongodb副本集

    MongoDB 复制(副本集) MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您 ...

  8. 行为驱动:Cucumber + Selenium + Java(二) - 第一个测试

    在上一篇中,我们搭建好了Selenium + Cucumber + Java的自动化测试环境,这一篇我们就赶紧开始编写我们的第一个BDD测试用例. 2.1 创建features 我们在新建的java项 ...

  9. 通过钩子程序跨程序关闭Window

    需求: 在实际场景中会有自身程序在调用第三方的动态库过程中,因为第三方的动态库弹框导致线程阻塞,必须手动将弹窗关闭后才能回到自身程序的主线程中. 最简单的场景就是很多自助设备,本身是没有固定操作员的, ...

  10. 【Go】string 优化误区及建议

    原文链接: https://blog.thinkeridea.com/201902/go/string_ye_shi_yin_yong_lei_xing.html 本文原标题为 <string ...