当前位置:网站首页>51nod2884
51nod2884
2022-08-08 23:29:00 【Lqingyyyy】

水题 阿 就是先求出 最短路树 然后 从树的底端开始删点就好了
#include<iostream>
#include<queue>
#include<utility>
#include<cstring>
#include<algorithm>
#include<vector>
#define x first
#define y second
using namespace std;
const int N = 3e5 + 10,M = 2e6 + 10;
typedef long long ll;
typedef pair<ll,int> PII;
vector<vector<int> > v(N);
int head[N],to[M],last[M],w[M],cnt = 1;
void add(int a,int b,int c){
to[++cnt] = b;
w[cnt] = c;
last[cnt] = head[a];
head[a] = cnt;
}
int flag[N];ll dist[N],pre[N];
void dij(){
memset(dist,0x3f,sizeof dist);
priority_queue<PII,vector<PII>,greater<PII> >q;
q.push({
0,1});
dist[1] = 0;
while(q.size()){
PII p = q.top();
q.pop();
if(flag[p.y]) continue;
flag[p.y] = 1;
for(int i = head[p.y]; i != -1; i = last[i]){
int j = to[i];
if(dist[j] > dist[p.y] + w[i]){
dist[j] = dist[p.y] + w[i];
pre[j] = i;
q.push({
dist[j],j});
}
}
}
}
int dep[N];
void dfs(int x,int lastt){
dep[x] = dep[lastt] + 1;
for(auto j : v[x]){
if(j == lastt) continue;
dfs(j,x);
}
}
int main(){
int n,m,k;
cin >> n >> m >> k;
memset(head,-1,sizeof head);
for(int i = 1; i <= m; i++){
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
dij();
for(int i = 2; i <= n; i++){
v[to[pre[i] ^ 1]].push_back(i);
}
dfs(1,0);
priority_queue<PII,vector<PII>,less<PII> > q;
for(int i = 2; i <= n; i++){
if(pre[i] != 0)
q.push({
dep[i],pre[i] / 2});
}
while(q.size() > k){
q.pop();
}
cout << min((int)q.size(),k) << endl;
while(q.size()){
printf("%d ",q.top().y);
q.pop();
}
return 0;
}
边栏推荐
猜你喜欢

iptables firewall content full solution

Manacher(求解最长回文子串)

(2022牛客多校五)D-Birds in the tree(树形DP)

2022杭电多校六 1007-Shinobu loves trip(同余方程)

2021 RoboCom 世界机器人开发者大赛-本科组(决赛)7-4猛犸不上 Ban(最短路)

洛谷P4197 Peaks 线段树合并

51nod 1830

ndk和JNI的使用初探

Kubernetes 实现 CI/CD 发布流程

Use Mongoose populate to implement multi-table associative storage and query, with complete code included
随机推荐
ArrayAccess 接口用处
用工具实现 Mock API 的整个流程
Virtual router redundancy protocol VRRP - double-machine hot backup
Oracle 锁表,如何解锁
动手写prometheus的exporter-01-Gauge(仪表盘)
不躺平,然后做到极致,就是最大的“安全感”
[Tensorflow2] Some interface changes of tensorflow1.x-tensorflow2.x
树莓派wiringPi库的使用补充
makefile automatically compiles C files in directories and subdirectories
抽象内部类
Small program figure display banner
stm32 uses serial port to receive idle interrupt + dma to achieve variable length dma reception
【PP-YOLOv2】测试自定义的数据集
机器学习建模高级用法!构建企业级AI建模流水线
JS中的预编译(AO、GO详解)
(2022牛客多校五)C-Bit Transmission(思维)
Kubernetes 实现 CI/CD 发布流程
(2022杭电多校三)1009.Package Delivery(贪心)
stm32 利用 串口接收空闲中断 + dma 实现不定长度dma 接收
(2022牛客多校四)H-Wall Builder II(思维)