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

题目大意:给定你一个包含n个点m条边的无向图,现在最多在图中保留k条边,问怎么删除多的边,使得图中良好的节点数最多,求出保留在图中的边的数量和编号。
良好的节点定义为:删除某条边后该点到点1的最短距离不变。
思路:先求出所有点到点1的最短距离,之后再bfs一遍,若遍历到某一节点时的距离等于该点到点1的最短距离就将该条边加进去,直到添加到k条边或者遍历结束。(虽然过了但是还是觉得有有的情况好像过不了,但是没想出来...可能数据还有点水..)
一开始INF值设小了WA了四次。。。 INF值设置1e15即可,因为边的权值很大所以上限需要很大。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> P;
const int maxn = 3e5+;
const LL INF = 1e18;
vector<int>ans;
int n, m, k;
struct node{
LL to,cost;
node() {}
node(LL a, LL b) :to(a), cost(b) {}
};
vector<node> e[maxn];
LL vis[maxn], f[maxn], dis[maxn];
map<P,LL>mp;
void SPFA(int s)
{
for (int i = ; i < maxn; i++) {
vis[i] = ; f[i] = ;
dis[i] = INF;
}
dis[s] = ;
vis[s] = ; f[s]++;
queue<int>Q;
Q.push(s);
while (!Q.empty()) {
int t = Q.front(); Q.pop();
vis[t] = ;
for (int i = ; i < e[t].size(); i++) {
LL tmp = e[t][i].to;
if (dis[tmp] > dis[t] + e[t][i].cost) {
dis[tmp] = dis[t] + e[t][i].cost;
if (!vis[tmp]) {
vis[tmp] = ;
Q.push(tmp);
if (++f[tmp] > n)return;
}
}
}
}
return;
}
void BFS(LL x)
{
ans.clear();
queue<node>Q;
memset(vis,,sizeof(vis));
Q.push(node(x,));
vis[x] = ;
while(!Q.empty()&&ans.size()<k){
node dep = Q.front();Q.pop();
for(int i=;i<e[dep.to].size();i++){
LL to = e[dep.to][i].to;
if(ans.size()>n-&&ans.size()<k){
ans.push_back(mp[make_pair(dep.to,to)]);
continue;
}
if(ans.size()==k)return;
if(vis[to])continue;
if((dep.cost+e[dep.to][i].cost)>dis[to])continue;
ans.push_back(mp[make_pair(dep.to,to)]);
vis[to] = ;
Q.push(node(to,dep.cost+e[dep.to][i].cost));
if(ans.size()==k)return;
}
}
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> n >> m >> k) {
mp.clear();
for(int i=;i<=n;i++)e[i].clear();
for (LL a, b, c, i = ; i <= m; i++) {
cin >> a >> b >> c;
e[a].push_back(node(b, c));
e[b].push_back(node(a, c));
mp[make_pair(a,b)] = i;
mp[make_pair(b,a)] = i;
// cout<<mp[make_pair(a,b)]<<endl;
}
if(k==){
cout<<""<<endl;
continue;
}
SPFA();
BFS();
cout<<ans.size()<<endl;
for(int i=;i<ans.size()-;i++)
cout<<ans[i]<<" ";
cout<<ans[ans.size()-]<<endl;
}
return ;
}
Educational Codeforces Round 54 (Rated for Div. 2) D Edge Deletion (SPFA + bfs)的更多相关文章
- Educational Codeforces Round 54 (Rated for Div. 2) D:Edge Deletion
题目链接:http://codeforces.com/contest/1076/problem/D 题意:给一个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 ...
随机推荐
- 利用Factory-boy和sqlalchemy来批量生成数据库表数据
测试过程中免不了要构造测试数据,如果是单条数据,还比较简单,但如果是批量数据,就比较麻烦了. 最近看到Factory_boy这个python第三方库,它通过SQLAlchemyModelFactory ...
- Linux之Shell1
1.输出命令:echo echo [选项] [输出内容] : -e 支持反斜线控制的字符转换.(类似于C语言的\) \\ 输出\本身 \t Tab键 \n 换行符 \f 换页符 ...
- malloc: *** error for object 0x10a291df8: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
malloc_error_break错误: .You'll find out what the object is when you break in the debugger. Just look ...
- Effective Modern C++:01类型推导
C++的官方钦定版本,都是以ISO标准被接受的年份命名,分别是C++98,C++03,C++11,C++14,C++17,C++20等.C++11及其后续版本统称为Modern C++. C++11之 ...
- 基于GD库的php验证码类(支持中英文字体、背景、干扰点线、扭曲…….)
转自:http://www.blhere.com/1168.html 12345678910111213141516171819202122232425262728293031323334353637 ...
- JavaScript--tab栏切换效果
tab栏切换效果: <!DOCTYPE html> <html> <head lang="en"> <meta charset=" ...
- kubernetes1.4新特性:支持Docker新特性
(一)背景资料 在Kubernetes1.2中这个第三方组件就是go-dockerclient,这是一个GO语言写的docker客户端,支持Dockerremote API,这个项目在https:// ...
- 你在用 JWT 代替 Session?
现在,JSON Web Tokens (JWT) 是非常流行的.尤其是 Web 开发领域. 流行 安全 稳定 易用 支持 JSON 所有这些因素,令 JWT 名声大振. 但是,今天我要来说说使用 JW ...
- .Net Core,VUE,VS Code,Sql Sugar,Element UI学习笔记
1..Net Core的目的是跨平台,并主要目标是作为服务端开发使用.从3.0开始,引入了Winfrom和WPF. 2..Net Core可以引用.Net Framework生成的dll和exe,不限 ...
- nodejs启本地服务器
https.js var PORT = 8666;// var http = require('http'); var url=require('url'); var fs=require('fs') ...