本文主要是介绍Codeforces Contest 1076 problem D Edge Deletion —— dijkstra的一些优化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
You are given an undirected connected weighted graph consisting of n vertices 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 k edges 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 e distinct 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.
Examples
inputCopy
3 3 2
1 2 1
3 2 1
1 3 3
outputCopy
2
1 2
inputCopy
4 5 2
4 1 8
2 4 1
2 1 3
3 4 9
3 1 5
outputCopy
2
3 2
题意:
给你n个点,m条边,你需要删除一些边使得剩下的边的数量小于等于k,并且我们规定,一个点是“好的”是在删边以后1到这个点的最短距离等于删边之前的最短距离,让你求出需要剩下哪些边使得“好的”的路最多。
题解:
dijkstra求出1到所有点的距离,之后从第一个点出发,看看与它相邻的点有哪些是之前就是最短的,然后放到队列里面,同时ans数组push进去这条边的id,对于这题有一些优化,首先最大的优化是这个:
dijkstra的时候不要用vis数组纪录,而是在pop的时候看这一个状态的点过来的最短路是否等于当前的最短路,如果不是就说明它之后有状态比它更优,还有就是不要把自定义结构体放到队列里,用pa会快,不要用map,直接将这条边的id放到结构体里,还有就是查询和dijkstra可以一起做。
分开做的情况:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pa pair<ll,int>
const int N=3e5+5;
const ll inf=1e17;
struct node
{int to,next,id;ll val;
}e[N*2];
int head[N],cnt;
void add(int x,int y,ll w,int id)
{e[cnt].to=y;e[cnt].next=head[x];e[cnt].val=w;e[cnt].id=id;head[x]=cnt++;
}
ll dis[N];
int vis[N],last[N];
vector<int>ans;
void dijkstra()
{priority_queue<pa>Q;dis[1]=0;Q.push({0,1});while(!Q.empty()){pa u=Q.top();Q.pop();if(-u.first!=dis[u.second])continue;for(int i=head[u.second];~i;i=e[i].next){int v=e[i].to;ll w=e[i].val;if(dis[v]>dis[u.second]+w){dis[v]=dis[u.second]+w;Q.push({-dis[v],v});}}}
}
void check(int x)
{priority_queue<pa>Q;dis[1]=0;Q.push({0,1});vis[1]=1;while(!Q.empty()&&x){pa u=Q.top();Q.pop();for(int i=head[u.second];~i;i=e[i].next){int v=e[i].to;ll w=e[i].val;if(vis[v])continue;if(dis[v]==dis[u.second]+w){Q.push({-dis[v],v});vis[v]=1;ans.push_back(e[i].id);x--;}if(x<=0)break;}}
}
int main()
{//freopen("in.txt","r",stdin);memset(head,-1,sizeof(head));for(int i=1;i<N;i++)dis[i]=inf;int x,y;ll w;int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=m;i++){scanf("%d%d%lld",&x,&y,&w);add(x,y,w,i),add(y,x,w,i);}dijkstra();check(k);printf("%d\n",ans.size());for(int i=0;i<ans.size();i++)printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');return 0;
}
合起来的情况:就是加一个数组在搜的时候就记录最优解的id
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pa pair<ll,int>
const int N=3e5+5;
const ll inf=1e17;
struct node
{int to,next,id;ll val;
}e[N*2];
int head[N],cnt;
void add(int x,int y,ll w,int id)
{e[cnt].to=y;e[cnt].next=head[x];e[cnt].val=w;e[cnt].id=id;head[x]=cnt++;
}
ll dis[N];
int vis[N],last[N];
vector<int>ans;
void dijkstra(int x)
{priority_queue<pa>Q;dis[1]=0;Q.push({0,1});while(!Q.empty()&&x){pa u=Q.top();Q.pop();if(-u.first!=dis[u.second])continue;if(last[u.second])x--,ans.push_back(last[u.second]);for(int i=head[u.second];~i;i=e[i].next){int v=e[i].to;ll w=e[i].val;if(dis[v]>dis[u.second]+w){dis[v]=dis[u.second]+w;Q.push({-dis[v],v});last[v]=e[i].id;}}}
}
int main()
{//freopen("in.txt","r",stdin);memset(head,-1,sizeof(head));for(int i=1;i<N;i++)dis[i]=inf;int x,y;ll w;int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=m;i++){scanf("%d%d%lld",&x,&y,&w);add(x,y,w,i),add(y,x,w,i);}dijkstra(k);printf("%d\n",ans.size());for(int i=0;i<ans.size();i++)printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');return 0;
}
这篇关于Codeforces Contest 1076 problem D Edge Deletion —— dijkstra的一些优化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!