CodeForces - 1076D Edge Deletion 最短路标记边

You are given an undirected connected weighted graph consisting of nn vertices and mmedges. 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 

题意:n个点 m个边 取最多k个边 最多可以取多少点  并且保证1到各点的最短路不变

题解:跑一边最短路,记录每个点最后是通过哪个边更新到的,每次到达一个点,把得到他的边记录下来即可

不知道为什么在优先队列不能用dis数组判断 一直错在第7个 坑死

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+10;
#define INF 1e18+10
int n,m,k;
int pid[N];
ll dis[N];
bool vis[N];
struct node1{
	int to,id;
	ll d;
	node1(int to_,ll d_,int id_):to(to_),d(d_),id(id_){}
};
struct node2{
	int id;
	ll l;
	node2(int id_,ll l_):id(id_),l(l_){}
	bool operator <(const node2 &x)const{
		return l>x.l;
	}
};
vector<node1> v1[N];
int cnt,ans[N];
void DIJ()
{
	for(int i=1;i<=n;i++) dis[i]=INF,vis[i]=0;
	dis[1]=0;
	priority_queue<node2> q;
	q.push(node2(1,0));
	cnt=-1;
	while(!q.empty())
	{
		node2 now=q.top();q.pop();
		if(vis[now.id]) continue;
		vis[now.id]=1;
		ans[++cnt]=pid[now.id];
		if(cnt==k) return;
		for(int i=0;i<v1[now.id].size();i++)
		{
			int to=v1[now.id][i].to;
			if(vis[to]==0&&dis[to]>dis[now.id]+v1[now.id][i].d)
			{
				dis[to]=dis[now.id]+v1[now.id][i].d;
				pid[to]=v1[now.id][i].id;
				q.push(node2(to,dis[to]));
			}
		}
	}
}
int main()
{
	int x,y;
	ll z;
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%lld",&x,&y,&z);
		v1[x].push_back(node1(y,z,i));
		v1[y].push_back(node1(x,z,i));
	}
	k=min(k,n-1);
	DIJ();
	printf("%d\n",k);
	if(k)
	{
		for(int i=1;i<=k;i++)
			printf("%d%c",ans[i]," \n"[i==k]);
	}
	else printf("\n");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值