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

20 篇文章 0 订阅

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;
}

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值