Educational Codeforces Round 54 (Rated for Div. 2) D:Edge Deletion
题目链接:http://codeforces.com/contest/1076/problem/D
题意:给一个n个点,m条边的无向图。要求保留最多k条边,使得其他点到1点的最短路剩余最多。
思路:当找到单源最短路后,将其转换为一个所有点到点1都是最短路的树状结构,利用贪心确定所要保留的K条边(找离根最近的边,利用BFS)。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
//#define local using namespace std; const int MOD = 1e9+;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 3e5 + ;
const int maxedge = 3e5 + ; struct qnode {
int v;
ll c;
qnode(int v=,ll c=) : v(v), c(c) {}
bool operator < (const qnode &r)const {//priority_queue 默认从大到小排列
return c > r.c;
}
}; struct Edge {
int v, w, pre;
} edge[maxedge*]; int point[maxn]; //point[i] = -1
int cnt;
bool vis[maxn]; // already init in the dijkstra
ll dist[maxn]; // already init in the dijkstra, overflow!
vector <int> v1[maxn];
vector <int> v2[maxn];
struct Path {
int u, e;
}path[maxn]; void AddEdge(int u, int v, int w) {
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].pre = point[u];
point[u] = cnt++;
} void Dijkstra(int n,int start) {
memset(vis,false,sizeof(vis));
memset(dist, 0x3f, sizeof(dist));
priority_queue <qnode> que;
while(!que.empty()) que.pop();
dist[start] = ;
que.push(qnode(start, ));
qnode tmp;
while(!que.empty()) {
tmp = que.top(); que.pop();
int u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(int i = point[u]; i != -; i = edge[i].pre) {
int v = edge[i].v;
int w = edge[i].w;
if(!vis[v] && dist[v]>dist[u]+w) {
dist[v] = dist[u]+w;
que.push(qnode(v, dist[v]));
path[v].u = u;
path[v].e = i;
}
}
}
} void init() {
cnt = ;
memset(point, -, sizeof(point));
} int main() {
#ifdef local
if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!\n");
#endif int n, m, k;
scanf("%d%d%d", &n, &m, &k);
init();
for (int i = ; i < m; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
AddEdge(u, v, w);
AddEdge(v, u, w);
}
Dijkstra(n, );
for (int i = ; i <= n; ++i) {
v1[path[i].u].push_back(i);
v2[path[i].u].push_back(path[i].e);
}
queue<int> q;
q.push();
int ans = ;
int Ans[maxn];
while (q.size()) {
int tmp = q.front();
q.pop();
for (int j = ; j < v1[tmp].size() && ans < k; ++j) {
q.push(v1[tmp][j]);
Ans[ans++] = v2[tmp][j]/;
}
}
printf("%d\n", ans);
for (int i = ; i < ans; ++i) {
if (i)
printf(" %d", Ans[i]+);
else printf("%d", Ans[i]+);
}
printf("\n");
#ifdef local
fclose(stdin);
#endif
return ;
}
Educational Codeforces Round 54 (Rated for Div. 2) D:Edge Deletion的更多相关文章
- Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)
题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...
- Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)
第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...
- Educational Codeforces Round 54 (Rated for Div. 2) Solution
A - Minimizing the String solved 题意:给出一个字符串,可以移掉最多一个字符,在所有可能性中选取一个字典序最小的. 思路:显然,一定可以移掉一个字符,如果移掉的字符的后 ...
- Educational Codeforces Round 54 (Rated for Div. 2) DE
D 给出一个无向图,需要删去一些边,想知道最后能有多少个点到1的距离还是过去那么短 如果求一个最短路,然后从删边的角度看,看起来很难做,但是如果从零开始加边就会有做法,如同prim那样,先加入和1直接 ...
- Educational Codeforces Round 54 (Rated for Div. 2) ABCD
A. Minimizing the String time limit per test 1 second memory limit per test 256 megabytes Descriptio ...
- Educational Codeforces Round 96 (Rated for Div. 2) D. String Deletion (思维)
题意:有一个\(01\)串,每次操作要先删除一个位置上的元素,然后删除相同前缀和,直到字符串被删完,问最多能操作多少次. 题解: 对于一个长度大于\(1\)的相同前缀,我们最多只能对它操作一次,然后就 ...
- Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] 给你 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
随机推荐
- Python面向对象基础知识
面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实例用于调用 ...
- unity解压缩zip发布后的一些问题
前段时间项目需要,搞了下zip的解压缩问题,也是利用ICSharpCode.SharpZipLib.dll来处理的zip,这里说下之前遇到的坑(这里提供我用的这个库ICSharpCode.SharpZ ...
- jquery.validate.js使用实例
一.常用方式: $('form').validate({ rules: {}, messages: { }, submitHandler: function () {}) ...
- 在eclipse中安装groovy插件
在eclipse中安装groovy插件详细步骤: step 1:检查自己的eclipse版本:在help->About Eclipse中查看: step 2:进入 https://github. ...
- ES6常用方法总结
1.声明变量用let,声明常量用const(定义唯一的值),都没有预解释,也不存在变量提升: 2.箭头函数:有如下两种写法 1).表达式(函数体只有一行代码) a).let fn = p => ...
- Tkinter模块:Grid几何管理器
Tkinter模块是Python的标准库模块之一,也是使用Python语言进行图形化用户界面(GUI)开发的基础. 本文介绍一下Tkinter模块的Grid几何管理器. 使用VB.MFC进行GUI开发 ...
- nginx配置支持http2
1.简介 nginx 配置支持http2.目前大多数网站都是http1.1(如果你没有特别配置过的话) 一切都是为了访问更快. 2.如何查看自己网站的http版本 最简单的方法就F12啊,我这里是火狐 ...
- Swing学习2——图标添加Icon接口使用
废话没有,看代码. 主要就是通过实现Icon接口在标签添加一个圆形图标,并在框架中显示. package com.sword.swing_test; import javax.swing.*; imp ...
- 1--Test NG--常见测试和注解
第一:注解 (1)@test (2)@BeforeMethod,@AfterMethod (3)@BeforeClass,@AfterClass (4)@BeforeSuite,@AfterSuite ...
- Spring - AOP简介与图示
[1]AOP (Aspect-Oriented Programming, 面向切面编程),是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) ...