题目链接: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的更多相关文章

  1. Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)

    题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号. 良好的节点定义为:删除某条边后该点到点1的最短距离 ...

  2. Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)

    第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...

  3. Educational Codeforces Round 54 (Rated for Div. 2) Solution

    A - Minimizing the String solved 题意:给出一个字符串,可以移掉最多一个字符,在所有可能性中选取一个字典序最小的. 思路:显然,一定可以移掉一个字符,如果移掉的字符的后 ...

  4. Educational Codeforces Round 54 (Rated for Div. 2) DE

    D 给出一个无向图,需要删去一些边,想知道最后能有多少个点到1的距离还是过去那么短 如果求一个最短路,然后从删边的角度看,看起来很难做,但是如果从零开始加边就会有做法,如同prim那样,先加入和1直接 ...

  5. 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 ...

  6. Educational Codeforces Round 96 (Rated for Div. 2) D. String Deletion (思维)

    题意:有一个\(01\)串,每次操作要先删除一个位置上的元素,然后删除相同前缀和,直到字符串被删完,问最多能操作多少次. 题解: 对于一个长度大于\(1\)的相同前缀,我们最多只能对它操作一次,然后就 ...

  7. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. net core 随笔

    UseApplicationInsights  这个有用到azure 才有用, 平时没用的话可以去掉. 遥测. 上下文指的是 进程间占有的资源空间. 当一个进程时间片到了或者资缺的时候就会让出CPU ...

  2. js插件ztree使用

    最新给公司后台写了一个配置页面,在网上搜到一个js插件ztree,记录一下使用心得. 首先说一下ztree官网,好多方法我都是从官网api上学习的,官网地址http://www.treejs.cn/v ...

  3. java基础 易忘易混点复习1

    原码 反码 补码 原码 正数的原码最高位是0 负数的原码最高位是1 例如:+7 0 0000111 -7 1 0000111 反码 正数的反码与原码相同 负数的反码相比原码 符号位不变,数值位取反 例 ...

  4. jquery slideDown 控制div出现的方向

    .custom-popup { position: absolute; /*top: 0;*/ 上向下 ; 下向上 ; ; display: none; width: 100%; height: 10 ...

  5. 移动端设备中1px适配

    方式1:伪类+transform实现,主要用transform中的scale缩放,缩放默认中心点是以x,y轴的50%处,因此需要用transform-origin调整中心点 html代码: <d ...

  6. 关于几天来研究使用css3动画的一点总结

    1. 为避免合成线程频繁计算导致性能降低, 使用transform属性,尽量避免使用width / height / padding / left 等. 2. 侧边栏下阴影遮罩层动画, 如果用back ...

  7. dubbo入门学习笔记之入门demo(基于普通maven项目)

    注:本笔记接dubbo入门学习笔记之环境准备继续记录; (四)开发服务提供者和消费者并让他们在启动时分别向注册中心注册和订阅服务 需求:订单服务中初始化订单功能需要调用用户服务的获取用户信息的接口(订 ...

  8. 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析 20165326

    恶意代码分析 实践目标 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systra ...

  9. mysql 的存储过程_多字段

    mysql 的存储过程 一.准备工作 新建一个表 /*Navicat MySQL Data Transfer Source Server : localhost_3306Source Server V ...

  10. winform devexpress 用法汇总

    废话不多说先上图 1.封装分页控件 qrcodeOnPage1.SearchData(gridControl2, IDataPage, sWhere, "", "tb_o ...