首页 技术 正文
技术 2022年11月16日
0 收藏 709 点赞 2,612 浏览 1314 个字

题目链接:https://vjudge.net/problem/CodeForces-1196F

题意:从图中找出第K短的最短路,最短路:从一个点到另一个的最短距离。

思路:题目说了,每两个点之间的边小于等于1,那么如果我们只取K条边,

那么顶点数  V∈[K,2K],所以我们一定可以在K条边中的到第K短的最短路。

当然我们先要把所有变来一个sort,取权值小的K条边。

之后跑一个floyd就可以了,然后把所有最短路存下来,找出第K小的最短路。

ps(这里的编号很方便,我是参考另一个大佬的,这里说明一下,当然也可以和我之前一样,

来个计数的慢慢编号)。


 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std; typedef long long LL;
#define inf (1LL << 60)
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = (int)2e5 + ;
LL tmp[N];
LL dis[][];
int n,m,k;
map<int, int> s;
int tot; struct node{
int u;
int v;
int w; bool friend operator < (node a,node b){
return a.w < b.w;
} }o[N]; int ID(int x){
if(s.count(x)) return s[x];
return s[x] = ++tot;
} int main(){ ios::sync_with_stdio(false);
cin.tie(); cin >> n >> m >> k; int u,v,w,l = ;
//存边
rep(i,,m){
cin >> u >> v >> w;
o[l].u = u;
o[l].v = v;
o[l++].w = w;
}
//sort边,取前K条
sort(o,o + m); rep(i,,) rep(j,,){
if(i == j) dis[i][j] = ;
else dis[i][j] = inf;
} //建图
rep__(i,,min(m,k)){
dis[ID(o[i].u)][ID(o[i].v)] = dis[ID(o[i].v)][ID(o[i].u)] = o[i].w;
} rep(k,,tot) rep(i,,tot) rep(j,,tot){
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
} l = ;
rep(i,,tot) rep(j,i + ,tot){
tmp[l++] = dis[i][j];
} sort(tmp, tmp + l); cout << tmp[k - ] << endl; getchar();getchar(); return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902