codeforces 1196F K-th Path

K-th Path
题目大意
一个 n n n 个点 m m m 条边的无向连通图,给你 n n n 个点, m m m 条边,输出无向图中单源最短路径第 k k k 小的值。 m m m 条边,给出点 x x x y y y 以及权值 w w w,点 x x x y y y 和点 y y y x x x 只会计算一次。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=(int)1e6+100;
int n,m,k,q[maxn],cnt,tot,h[maxn],p,vis[maxn],v,num;
ll dis[maxn],ans[maxn];
queue<int>Q;

struct kk{
	int x,y;ll w;
	bool operator<(const kk &a)const{
	    return w<a.w;
    }
}a[maxn];

struct node{
    int to,next;
    ll w;
}edge[maxn<<1];

void add(int x,int y,ll z)
{
    edge[++tot].to=y;
    edge[tot].next=h[x];
    edge[tot].w=z;
    h[x]=tot;
}
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=m;++i) scanf("%d%d%lld",&a[i].x,&a[i].y,&a[i].w);
	sort(a+1,a+m+1);
	for(int i=1;i<=k;++i) q[++cnt]=a[i].x,q[++cnt]=a[i].y,add(a[i].x,a[i].y,a[i].w),add(a[i].y,a[i].x,a[i].w);
	sort(q+1,q+cnt+1);
	cnt=unique(q+1,q+cnt+1)-q-1;
	for(int i=1;i<=cnt;++i){
		int u=q[i];
		for(int j=1;j<=cnt;++j) dis[q[j]]=(ll)1e18;
		dis[u]=0; Q.push(u);
		while (!Q.empty()){
			p=Q.front(),Q.pop();vis[p]=0;
			for (int k=h[p];k;k=edge[k].next)
				if(dis[v=edge[k].to]>dis[p]+edge[k].w){
					dis[v]=dis[p]+edge[k].w;
					if (!vis[v]) Q.push(v),vis[v]=1;
				}
		}
		for(int j=1;j<=cnt;++j) if(i<j) ans[++num]=dis[q[j]];
	}
	sort(ans+1,ans+num+1);
	printf("%lld\n",ans[k]);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸愉信奥

谢谢亲的支持,我会继续努力啦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值