Codeforces - D. Edge Deletion

You are given an undirected connected weighted graph consisting of n vertices and m edges. Let’s denote the length of the shortest path from vertex 1 to vertex i as di.

You have to erase some edges of the graph so that at most k edges remain. Let’s call a vertex i good if there still exists a path from 1 to i with length di after erasing the edges.

Your goal is to erase the edges in such a way that the number of good vertices is maximized.

Input
The first line contains three integers n, m and k (2≤n≤3⋅105, 1≤m≤3⋅105, n−1≤m, 0≤k≤m) — the number of vertices and edges in the graph, and the maximum number of edges that can be retained in the graph, respectively.

Then m lines follow, each containing three integers x, y, w (1≤x,y≤n, x≠y, 1≤w≤109), denoting an edge connecting vertices x and y and having weight w.

The given graph is connected (any vertex can be reached from any other vertex) and simple (there are no self-loops, and for each unordered pair of vertices there exists at most one edge connecting these vertices).

Output
In the first line print e — the number of edges that should remain in the graph (0≤e≤k).

In the second line print e distinct integers from 1 to m — the indices of edges that should remain in the graph. Edges are numbered in the same order they are given in the input. The number of good vertices should be as large as possible.

Examples
inputCopy
3 3 2
1 2 1
3 2 1
1 3 3
outputCopy
2
1 2
inputCopy
4 5 2
4 1 8
2 4 1
2 1 3
3 4 9
3 1 5
outputCopy
2
3 2


Obvious approach, we need to find the shortest side of the road, that we find the shortest path tree.

Then there from the start of k paths.

AC Code:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+10;
int n,m,k,pre[N],d[N],vis[N];
int head[N],nex[N<<1],to[N<<1],w[N<<1],id[N<<1],tot;
vector<int> res;
inline void ade(int a,int b,int c,int d){
	to[++tot]=b; nex[tot]=head[a]; w[tot]=c; id[tot]=d;	head[a]=tot;
}
inline void add(int a,int b,int c,int d){ade(a,b,c,d);	ade(b,a,c,d);}
void Dijkstra(){
	priority_queue<pair<int,int> > q; memset(d,0x3f,sizeof d); d[1]=0; q.push({0,1});
	while(q.size()){
		int u=q.top().second;	q.pop();
		if(vis[u])	continue;	vis[u]=1;
		for(int i=head[u];i;i=nex[i]){
			if(d[to[i]]>d[u]+w[i]){
				d[to[i]]=d[u]+w[i];	q.push({-d[to[i]],to[i]});
				pre[to[i]]=u;
			}
		}
	}
}
void bfs(){
	queue<int> q;	q.push(1);	memset(vis,0,sizeof vis);	vis[1]=1;
	while(q.size()){
		int u=q.front();	q.pop();
		for(int i=head[u];i;i=nex[i]){
			if(pre[to[i]]==u&&res.size()<k&&!vis[to[i]]){
				res.push_back(id[i]);	q.push(to[i]);	vis[to[i]]=1;	
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false); cin.tie(nullptr);
	cin>>n>>m>>k;
	for(int i=1,a,b,c;i<=m;i++)	cin>>a>>b>>c,add(a,b,c,i);
	Dijkstra();	bfs();
	cout<<res.size()<<'\n';
	for(int i=0;i<res.size();i++)	cout<<res[i]<<' ';
	return 0;
}
Published 476 original articles · won praise 241 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_43826249/article/details/104092451