[Codeforces 507E] Breaking Good
[题目链接]
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的更多相关文章
- CodeForces 507E Breaking Good 2维权重dij
Breaking Good 题解: 2维权重dij, 先距离最短, 后改变最小. 在这个题中, 如果要改变最小, 则让更多的可用边放进来. 然后可以用pre存下关键边. 代码: ...
- 【codeforces 507E】Breaking Good
[题目链接]:https://vjudge.net/contest/164884#problem/D [题意] 给你一张图; 图中有些路是完好的;但有些路还没修好; 先不管路有没有修好; 问你从起点到 ...
- cf 507E. Breaking Good
因为要求是在保证最短路的情况下花费是最小的,所以(先保证最短路设为S吧) 那么花费就是最短路上的新建边条数A+剩余拆掉边的条数B,而且总的原有好的边是一定的,所以,只要使得A尽量小,那么B就大,所以要 ...
- Codeforces Round #287 (Div. 2) E. Breaking Good 最短路
题目链接: http://codeforces.com/problemset/problem/507/E E. Breaking Good time limit per test2 secondsme ...
- 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 ...
- 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 ...
- Codeforces Breaking Good
Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes Breaking Good is a n ...
- 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 ...
- Codeforces Round #531 (Div. 3) C. Doors Breaking and Repairing (博弈)
题意:有\(n\)扇门,你每次可以攻击某个门,使其hp减少\(x\)(\(\le 0\)后就不可修复了),之后警察会修复某个门,使其hp增加\(y\),问你最多可以破坏多少扇门? 题解:首先如果\(x ...
随机推荐
- Computers(线性DP)
描述 Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, th ...
- [Docker]容器的隔离与限制
1.Docker事实 1)容器技术的兴起源于Pass技术的普及 2)Docker公司发布的Docker项目具有里程碑式的意义 3)Docker项目通过容器镜像解决了应用打包这个根本性难题 4)容器本身 ...
- 七牛云成功通过 CMMI3 认证
10 月 31 日,在上海七牛信息技术有限公司青岛会议室举行的 CMMI3 级认证结果发布会上,主任评估师王庆付老师和评估组向公司高层及参与评审的 EPG 成员及项目组成员郑重宣布:经过严格的现场审核 ...
- bzoj2277 [Poi2011]Strongbox
2277: [Poi2011]Strongbox Time Limit: 60 Sec Memory Limit: 32 MBSubmit: 498 Solved: 218[Submit][Sta ...
- 【HDOJ5640】King's Cake(数论)
题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...
- msp430入门编程25
msp430中C语言开发环境搭建 msp430入门学习 msp430入门编程
- 关于Linux内核学习的一点点总结
关于Linux内核学习的一点点总结 关键词:Linux, 操作系统,内核 博客列表 由反汇编C程序来理解计算机是如何工作的 通过分析一个简化版时间片轮转多道程序内核代码来认识操作系统中的进程调度 通过 ...
- 【Perl】perl正则表达式中的元字符、转义字符、量词及匹配方式
Linux平台上被广泛使用的正则表达式库PCRE - Perl-compatible regular expressions,从其名字即可知道,PCRE提供的是一套与Perl中相兼容的正则表达式. 元 ...
- Ubuntu 16.04安装QQ(不一定成功)
注意1:如果是刚新装的系统,可以正常安装,但是,如果你已经装了很多软件,千万不要安装,因为会把系统上一般的依赖包和你之前装的软件全部卸载掉!甚至将桌面Dock都会卸载!最终只能重装Ubuntu解决. ...
- 代码svn下载到本地后,关于数据库问题
代码svn下载到本地后,关于数据库问题 1.那我本地还用搭建相应的数据库么?答案:当然不用啦,本地系统里已经配置好了数据库的网络地址了,端口号,密码啥的.即使你代码运行在本地,依然可以将数据传输到服务 ...