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

3 篇文章 0 订阅
2 篇文章 0 订阅

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值