codeforces 1076 D. Edge Deletion(dijsktra+堆优化)

D. Edge Deletion
time limit per test2.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an undirected connected weighted graph consisting of nn vertices and mm edges. Let’s denote the length of the shortest path from vertex 11 to vertex ii as didi.

You have to erase some edges of the graph so that at most kk edges remain. Let’s call a vertex ii good if there still exists a path from 11 to ii with length didi 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 nn, mm and kk (2≤n≤3⋅1052≤n≤3⋅105, 1≤m≤3⋅1051≤m≤3⋅105, n−1≤mn−1≤m, 0≤k≤m0≤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 mm lines follow, each containing three integers xx, yy, ww (1≤x,y≤n1≤x,y≤n, x≠yx≠y, 1≤w≤1091≤w≤109), denoting an edge connecting vertices xx and yy and having weight ww.

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 ee — the number of edges that should remain in the graph (0≤e≤k0≤e≤k).

In the second line print ee distinct integers from 11 to mm — 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
input
3 3 2
1 2 1
3 2 1
1 3 3
output
2
1 2
input
4 5 2
4 1 8
2 4 1
2 1 3
3 4 9
3 1 5
output
2
3 2

思路:直接用dijsktra 的本质每次找到最短的路更新,找到k条最短路径即可,用优先队列优化找到最短边的过程,每次取出的边先判断一下是不是比先前找到的短。时间复杂度nlogn

#include
#define fi first
#define se second
#define FOR(a) for(int i=0;i
#define sc(a) scanf("%d",&a)
#define show(a) cout<
#define show2(a,b) cout<
#define show3(a,b,c) cout<
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17+10;
const int N = 1e6 + 10;
const ll mod = 998244353;

map<string, int>ml;



int c[N], vis[N], a[N], t, n, m, x, y, k;
char s[N];
int ex, ey,cnt;
ll dist[N];
vector<int> ans;


struct node
{
	ll x;//权值
	int y,id;//
	bool friend operator < (node a, node b)
	{
		return a.x>b.x;
	}
};
struct edge
{
	int to,id;//存相连边的信息
	ll cost;
};

vector<edge> ed[N];

void dij()
{
	for(int i=1;i<=n;i++) dist[i]=inf;
	priority_queue<node> q;
	dist[1]=0;
	q.push(node{0,1,0});

	while(q.size())
	{
		node now=q.top();q.pop();
		int v=now.y;
		if(dist[v]<now.x) continue;//现在的值已经比进堆的值小了,就直接跳过,不用更新它了

		if(ans.size()==k) break;
		if(now.id) ans.push_back(now.id);
		for(int i=0;i<ed[v].size();i++)
		{
			edge e=ed[v][i];
			if(dist[e.to]>dist[v]+e.cost)
			{
				dist[e.to]=dist[v]+e.cost;
				q.push(node{dist[e.to],e.to,e.id});//存达到点的距离,点,和该路编号
			}
		}
	}
}
int main()
{
	//ios::sync_with_stdio ( false );
	//cin.tie ( 0 );

	sc(n);sc(m);sc(k);
	int u,v;
	ll w;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%I64d",&u,&v,&w);
		ed[u].push_back(edge{v,i,w});
		ed[v].push_back(edge{u,i,w});
	}
	dij();
	cout<<ans.size()<<endl;
	for(int i=0;i<ans.size();i++)
	{
		printf("%d ",ans[i]);
	}




}

你可能感兴趣的:(最短路,模板)