题目链接:http://codeforces.com/contest/1076/problem/D

题目大意:

一个图N个点M条双向边。设各点到点1的距离为di,保证满足条件删除M-K条边之后使得到点1的距离仍为di的点数量最多的情况下,输出剩余的K条边的编号(按输入顺序)。

   (2≤n≤3⋅105, 1≤m≤3⋅105, n−1≤m, 0≤k≤m)

解题思路:太菜了没写出来。。。

参考自博客:https://www.cnblogs.com/Lubixiaosi-Zhaocao/p/9951711.html

用迪杰斯特拉在图中跑最短路,并且利用两个数组存被松弛节点的父节点和保存对应的边,然后用bfs贪心保留离源点近的边。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=3e5+;
int n,m,k;
struct qnode{
int v;
ll d;
qnode(int a,ll b):v(a),d(b){}
bool operator<(const qnode& x)const{
return d>x.d;
}
};
struct edge{
int id,v;
ll w;
edge(int a,int b,ll c):id(a),v(b),w(c){}
};
vector<edge> mp[maxn];
void add(int id,int u,int v,ll w)
{
mp[u].push_back(edge(id,v,w));
mp[v].push_back(edge(id,u,w));
}
int vis[maxn],pree[maxn],pret[maxn];
ll dis[maxn];
priority_queue<qnode> pq;
void dij()
{
memset(vis,,sizeof(vis));
memset(dis,0x3f3f3f3f,sizeof(dis));
dis[]=;
pret[]=;
pq.push(qnode(,));
while(!pq.empty())
{
qnode q=pq.top();
pq.pop();
int u=q.v;
if(vis[u]) continue;
vis[u]=;
for(int i=;i<mp[u].size();i++)
{
int id=mp[u][i].id;
int v=mp[u][i].v;
ll w=mp[u][i].w;
if(!vis[v]&&dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
pret[v]=u;
pree[v]=id;
pq.push(qnode(v,dis[v]));
}
}
}
}
vector<int> son[maxn];
queue<int> que;
vector<int> ans;
void bfs()
{
que.push();
while(!que.empty())
{
int u=que.front();
que.pop();
for(int i=;i<son[u].size();i++)
{
int v=son[u][i];
if(k>)
{
ans.push_back(pree[v]);
que.push(v);
k--;
}
else break;
}
}
} int main()
{
ios_base::sync_with_stdio(false); cin.tie();
cin>>n>>m>>k;
for(int i=;i<=m;i++)
{
int u,v;
ll w;
cin>>u>>v>>w;
add(i,u,v,w);
}
dij();
for(int i=;i<=n;i++)
son[pret[i]].push_back(i);
bfs();
cout<<ans.size()<<endl;
if(ans.size()==)
return ;
cout<<ans[];
for(int i=;i<ans.size();i++)
cout<<" "<<ans[i];
cout<<endl;
return ;
}

Codeforces1076D. Edge Deletion(最短路树+bfs)的更多相关文章

  1. CF1076D Edge Deletion 最短路树

    问题描述 Codeforces 洛谷(有翻译) 题解 最短路树,是一棵在最短路过程中构建的树. 在\(\mathrm{Dijkstra}\)过程中,如果最终点\(y\)是由点\(x\)转移得到的,则在 ...

  2. CF1076D Edge Deletion 最短路径树+bfs

    题目描述 You are given an undirected connected weighted graph consisting of n n n vertices and m m m edg ...

  3. Codeforces 1076D Edge Deletion(最短路树)

    题目链接:Edge Deletion 题意:给定一张n个顶点,m条边的带权无向图,已知从顶点1到各个顶点的最短路径为di,现要求保留最多k条边,使得从顶点1到各个顶点的最短距离为di的顶点最多.输出m ...

  4. CF1005F Berland and the Shortest Paths (树上构造最短路树)

    题目大意:给你一个边权为$1$的无向图,构造出所有$1$为根的最短路树并输出 性质:单源最短路树上每个点到根的路径 ,一定是这个点到根的最短路之一 边权为$1$,$bfs$出单源最短路,然后构建最短路 ...

  5. hdu 3409 最短路树+树形dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3409 参考博客:http://www.cnblogs.com/woaishizhan/p/318981 ...

  6. LA4080/UVa1416 Warfare And Logistics 最短路树

    题目大意: 求图中两两点对最短距离之和 允许你删除一条边,让你最大化删除这个边之后的图中两两点对最短距离之和. 暴力:每次枚举删除哪条边,以每个点为源点做一次最短路,复杂度\(O(NM^2logN)\ ...

  7. 51nod 1443 路径和树(最短路树)

    题目链接:路径和树 题意:给定无向带权连通图,求从u开始边权和最小的最短路树,输出最小边权和. 题解:构造出最短路树,把存留下来的边权全部加起来.(跑dijkstra的时候松弛加上$ < $变成 ...

  8. Berland and the Shortest Paths CodeForces - 1005F(最短路树)

    最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...

  9. Connections between cities HDU - 2874(最短路树 lca )

    题意: 给出n个点m条边的图,c次询问 求询问中两个点间的最短距离. 解析: Floyd会T,所以用到了最短路树..具体思想为: 设k为u和v的最近公共祖先 d[i] 为祖结点到i的最短距离  则di ...

随机推荐

  1. java中的定时任务小示例

    package package_1; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; ...

  2. vue单页面模板说明文档(3)

    Environment Variables Sometimes it is practical to have different config values according to the env ...

  3. JavaScript lastIndexOf() 方法

    <script type="text/javascript"> var str="0000.0000.0000.0000.0000.0000.0000.&qu ...

  4. AngularJS基于模块化的MVC实现

    AngularJS基于模块化的MVC实现 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...

  5. Java多线程1:进程与线程的概念、区别和联系

    一.进程的的概念 引用线程之前进程的概念: 进程是表示资源分配的基本单位,也是调度运行的基本单位.例如,用户运行自己的程序,系统就创建一个进程,并为它分配资源,包括内存空间.磁盘空间.I/O设备等.然 ...

  6. python数据结构与算法第三天【时间复杂度计算方法】

    最优时间复杂度(不可靠) 最坏时间复杂度(保证) 平均时间复杂度(平均状况) 不同语句的时间复杂度: (1)顺序语句:使用加法 (2)循环语句:使用乘法 (3)分支语句:使用坏时间复杂度 例如:如下代 ...

  7. SpringBoot Junit Maven JaCoCo

    写一下最近写单体测试的一些笔记. SrpingBoot的测试用例: @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = ...

  8. css3实现背景渐变

    #grad { background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /* Safari 5.1 - 6 ...

  9. C#/.Net判断是否为周末/节假日

    判断节假日请求的Api:http://tool.bitefu.net/jiari/ /// <summary> /// 判断是不是周末/节假日 /// </summary> / ...

  10. MySql获取树型结构的所有子节点

    stackoverflow的解决方案,亲测有效: SELECT * FROM person WHERE department IN (SELECT department_id FROM departm ...