codeforce 1076D Edge Deletion 最短路+搜索+dij标记在队

D. Edge Deletion

time limit per test

2.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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

Copy

3 3 2
1 2 1
3 2 1
1 3 3

Output

Copy

2
1 2 

Input

Copy

4 5 2
4 1 8
2 4 1
2 1 3
3 4 9
3 1 5

Output

Copy

2
3 2 

题意:给你一个无向图,求出1到其他点的最短路di,让你保留最多k条边其他的边删除,使得最多得点di不发生变化

思路:先求出1点到其他点的最短路,标记最短路的路径,然后从1点开始dfs或bfs找出k条或者n-1边就return;

反思:思路很好想,很容易想到先找对短路在找min(n-1,k)条边就好,但是无辜的我在64组测试数据T了无数次,最后毫无办法莽了一发dij标记在队(其实是dij不需要标记在队的,只是spfa才需要的,此题spfa被卡点了),然后跑的贼几把快300ms这题还给的2500ms,无语。

dij标记在队代码:

#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=3e5+5;
const int inf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const double e=exp(1.0);
const double pi=acos(-1);
const double eps=1e-6;
struct Edge{
    int id,to,w,next;
}edge[maxn<<1];
struct node{
    int pos;
    ll w;
    bool operator < (const node &t)const{
    return w > t.w;
    }
};
int n,m,k,cnt,head[maxn],last[maxn],ans[maxn];
ll dis[maxn];
bool use[maxn];
void addedge(int st,int en,int id,int w)
{
    edge[cnt].to=en;
    edge[cnt].w=w;
    edge[cnt].id=id;
    edge[cnt].next=head[st];
    head[st]=cnt++;
}
void dij()
{
    for(int i=1;i<=n;i++)dis[i]=linf,last[i]=-1;
    dis[1]=0ll;
    priority_queue<node>q;
    q.push((node){1,0ll});
    use[1]=1;
    while(!q.empty())
    {
        node now=q.top();
        q.pop();
        use[now.pos]=0;
        for(int i=head[now.pos];i!=-1;i=edge[i].next)
        {
            int next=edge[i].to;
            if(dis[next]>dis[now.pos]+(ll)edge[i].w)
            {
                dis[next]=dis[now.pos]+(ll)edge[i].w;
                if(!use[next])q.push((node){next,dis[next]}),use[next]=1;
                last[next]=now.pos;
            }
        }
    }
}
int num;
void bfs()
{
    queue<int>q;
    q.push(1);
    if(num==n||num==k)return ;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        for(int i=head[now];i!=-1;i=edge[i].next)
        {
            if(last[edge[i].to]==now){
                ans[num++]=edge[i].id;
                q.push(edge[i].to);
                if(num==n||num==k)return ;
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    int st,en,w;
    //memset(head,-1,sizeof head);
    for(int i=1;i<=n;i++)head[i]=-1,use[i]=0;
    cnt=0;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&st,&en,&w);
        addedge(st,en,i,w);
        addedge(en,st,i,w);
    }
    num=0;
    dij();
    bfs();
    printf("%d\n",num);
    for(int i=0;i<num;i++)
    printf("%d%c",ans[i],i==num-1?'\n':' ');
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章