CodeForces 1076D Edge Deletion(优化最短路)

Description

You are given an undirected connected weighted graph consisting of nvertices 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 kedges 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 edistinct 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.

Sample Input

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 
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
#define mem(a) memset(a,0,sizeof(a))
#define maxx 1e10
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
struct A
{
    ll ed,w,num;
    ll x,y;
    bool operator < (const A & a) const
    {
        return w>a.w;
    }
};
priority_queue <A> q;
ll n,m,k;
vector<A> a[300010];
set <ll> ans;
ll vis[300010];
queue<A> dis;
int main()
{
    cin>>n>>m>>k;
    for(ll i=1; i<=m; i++)
    {
        ll x,y,z;
        cin>>x>>y>>z;
        A t;
        t.ed=y;
        t.num=i;
        t.w=z;
        a[x].push_back(t);
        t.ed=x;
        t.num=i;
        t.w=z;
        a[y].push_back(t);
    }
    mem(vis);
    vis[1]=1;
    for(ll i=0; i<a[1].size(); i++)
    {
        A t=a[1][i];
        q.push(t);
    }
    for(ll l=0; l<k; l++)
    {
        ll minn=1e11;
        ll num=-1;
        ll e=-1;
        while(!q.empty())
        {
             A t=q.top();
             if(vis[t.ed]==0)
             {
                 minn=t.w;
                 num=t.ed;
                 e=t.num;
                 break;
             }
             else
                q.pop();
        }
        if(e==-1||num==-1)
        {
            break;
        }
        ans.insert(e);
        vis[num]=1;
        for(ll i=0; i<a[num].size(); i++)
        {
            A t=a[num][i];
            if(vis[t.ed]==0)
            {
                A w;
                w.ed=t.ed;
                w.w=t.w+minn;
                w.num=t.num;
                q.push(w);
            }
        }
    }
    cout<<ans.size()<<endl;
    ll i=0;
    set<ll>::iterator it;
    for(it=ans.begin(); it!=ans.end(); it++)
    {
        if(!i)
            cout<<*it;
        else
            cout<<" "<<*it;
        i++;
    }
    cout<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Septembre_/article/details/84979841