[题目链接]

https://codeforces.com/contest/507/problem/E

[算法]

首先BFS求出1到其余点的最短路 , N到其余点的最短路,记为distA[]和distB[]

显然 , 我们只需最大化求出的最短路上没有被破坏的边即可 , 不妨用f[i]表示现在在城市i , distA[i] + distB[i] = distA[N] , 最多还能经过几条没有被破坏的边

记忆化搜索即可

时间复杂度 : O(N)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + ;
const int inf = 1e9; struct edge
{
int to , state , id , nxt;
} e[MAXN << ];
struct info
{
int u , v , ns;
} res[MAXN]; int n , m , tot;
int u[MAXN],v[MAXN],state[MAXN],head[MAXN],dista[MAXN],distb[MAXN],f[MAXN];
pair<int,int> nxt[MAXN];
bool visited[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v,int s,int id)
{
tot++;
e[tot] = (edge){v,s,id,head[u]};
head[u] = tot;
}
inline void bfs1(int s)
{
queue< int > q;
dista[s] = ;
visited[s] = true;
q.push(s);
while (!q.empty())
{
int cur = q.front();
q.pop();
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to;
if (!visited[v])
{
visited[v] = true;
dista[v] = dista[cur] + ;
q.push(v);
}
}
}
}
inline void bfs2(int s)
{
queue< int > q;
distb[s] = ;
visited[s] = true;
q.push(s);
while (!q.empty())
{
int cur = q.front();
q.pop();
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to;
if (!visited[v])
{
visited[v] = true;
distb[v] = distb[cur] + ;
q.push(v);
}
}
}
}
inline int dp(int u)
{
if (u == n) return f[u] = ;
if (f[u] != -) return f[u];
f[u] = -inf;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to , st = e[i].state , id = e[i].id , value;
if (dista[u] + distb[v] + != dista[n]) continue;
if (st == )
{
value = dp(v) + ;
if (value > f[u])
{
f[u] = value;
nxt[u] = make_pair(v,id);
}
} else
{
value = dp(v);
if (value > f[u])
{
f[u] = value;
nxt[u] = make_pair(v,id);
}
}
}
return f[u];
}
inline void getpath(vector<int> &a)
{
int now = ;
while (nxt[now].first)
{
a.push_back(nxt[now].second);
now = nxt[now].first;
}
} int main()
{ read(n); read(m);
for (int i = ; i <= m; i++)
{
read(u[i]); read(v[i]); read(state[i]);
addedge(u[i],v[i],state[i],i);
addedge(v[i],u[i],state[i],i);
}
memset(visited,false,sizeof(visited));
bfs1();
memset(visited,false,sizeof(visited));
bfs2(n);
memset(f,,sizeof(f));
dp();
vector<int> path;
getpath(path);
int len = ;
memset(visited,false,sizeof(visited));
for (unsigned i = ; i < path.size(); i++)
{
visited[path[i]] = true;
if (state[path[i]] == ) res[++len] = (info){u[path[i]],v[path[i]],};
}
for (int i = ; i <= m; i++)
{
if (!visited[i] && state[i])
res[++len] = (info){u[i],v[i],};
}
printf("%d\n",len);
for (int i = ; i <= len; i++) printf("%d %d %d\n",res[i].u,res[i].v,res[i].ns); return ; }

[Codeforces 507E] Breaking Good的更多相关文章

  1. CodeForces 507E Breaking Good 2维权重dij

    Breaking Good 题解:         2维权重dij, 先距离最短, 后改变最小.   在这个题中, 如果要改变最小, 则让更多的可用边放进来. 然后可以用pre存下关键边.   代码: ...

  2. 【codeforces 507E】Breaking Good

    [题目链接]:https://vjudge.net/contest/164884#problem/D [题意] 给你一张图; 图中有些路是完好的;但有些路还没修好; 先不管路有没有修好; 问你从起点到 ...

  3. cf 507E. Breaking Good

    因为要求是在保证最短路的情况下花费是最小的,所以(先保证最短路设为S吧) 那么花费就是最短路上的新建边条数A+剩余拆掉边的条数B,而且总的原有好的边是一定的,所以,只要使得A尽量小,那么B就大,所以要 ...

  4. Codeforces Round #287 (Div. 2) E. Breaking Good 最短路

    题目链接: http://codeforces.com/problemset/problem/507/E E. Breaking Good time limit per test2 secondsme ...

  5. Codeforces Round #287 (Div. 2) E. Breaking Good [Dijkstra 最短路 优先队列]

    传送门 E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces Round #287 (Div. 2) E. Breaking Good 路径记录!!!+最短路+堆优化

    E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. Codeforces Breaking Good

    Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes Breaking Good is a n ...

  8. Doors Breaking and Repairing CodeForces - 1102C (思维)

    You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consi ...

  9. Codeforces Round #531 (Div. 3) C. Doors Breaking and Repairing (博弈)

    题意:有\(n\)扇门,你每次可以攻击某个门,使其hp减少\(x\)(\(\le 0\)后就不可修复了),之后警察会修复某个门,使其hp增加\(y\),问你最多可以破坏多少扇门? 题解:首先如果\(x ...

随机推荐

  1. 《大话设计模式》Python版代码实现

    上一周把<大话设计模式>看完了,对面向对象技术有了新的理解,对于一个在C下写代码比较多.偶尔会用到一些脚本语言写脚本的人来说,很是开阔眼界.<大话设计模式>的代码使用C#写成的 ...

  2. 莫队乱搞--BZOJ2038: [2009国家集训队]小Z的袜子(hose)

    $n \leq 50000$的$\leq 50000$的数字序列,$m \leq 50000$个询问,每次问一个区间中随机拿两次(不放回)拿到相同数字的概率,以既约分数形式输出. 莫队入门.把询问按“ ...

  3. vs2010 静态使用 opencv 2.46 库

    下载opencv2.46的库,假设解压到OpenCV246,设置如下: 在工程的c++的include目录下添加:OpenCV246\opencv\build\include 在工程的c++的lib目 ...

  4. python学习之-- logging模块

    logging模块功能:提供了标准的日志接口,可以通过它存储各种格式的日志.日志5个级别分:debug(),info(),warning(),error(),critical() logging.ba ...

  5. mybatis <!-- useGeneratedKeys="true"把新增加的主键赋值到自己定义的keyProperty(id)中 -->

    <!-- useGeneratedKeys="true"把新增加的主键赋值到自己定义的keyProperty(id)中 -->

  6. chrome插件vimium的安装和使用

    vimium工具的作用:使你脱离鼠标,使用键盘方便操作页面,默认对所有网站生效 1.chrome商店里有的,但是,我怎么安装,都不行 2.源码安装:http://vimium.github.io/ h ...

  7. 百亿级企业级 RPC 框架开源了!

    今天给大家介绍给一款性能卓越的 RPC 开源框架,其作者就是我推荐每个 Java 程序员都应该看的<Java 生态核心知识点整理>的原作者张玉龙. 说实话我第一次看到这个资料的时候,就感觉 ...

  8. hdoj 3351 Seinfeld 【栈的简单应用】

    Seinfeld Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. IntelliJ IDEA 基本配置入门

    前言:今天下载安装IntelliJ IDEA.随手创建了一个项目,运行Build提示错误. 与大多数用于开发JAVA的IDE类似,不做不论什么配置.编译是不会成功的.因此我尝试对IDEA的配置进行了一 ...

  10. 万恶之源:C语言中的隐式函数声明

    1 什么是C语言的隐式函数声明 在C语言中,函数在调用前不一定非要声明.如果没有声明,那么编译器会自己主动依照一种隐式声明的规则,为调用函数的C代码产生汇编代码.以下是一个样例: int main(i ...