Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )
图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输出最小修改值,否则输出"NO"。保证有s到t的路径,可能有重边。
建正反两个图,分别求出s和t的最短路径,用来判断一条边是不是在最短路径上,然后把最短路径取出来建一个图求割边。
不要用spfa,最坏O(VE),(出卡spfa数据的方法:Hard Test),会TLE 109(数据好啊),自以为是的加了个优化结果跑得更慢了,估计也是那组数据卡的吧,带重边的tarjan处理:前向星i^1!=fa,或者每条边再记录一个信息,不能用v!=fa来判断。。
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
#define fi first
#define se second
#define bug(x) cout<<#x<<'='<<x<<endl;
#define FOR(i,s,e) for(int i = s; i < e; i++) const int maxn = 1e5+;
const int maxe = maxn<<;
struct Graph
{
ll d[maxn];
int head[maxn],fro[maxe],to[maxe],nxt[maxe],ecnt;
ll w[maxe];
void init() { memset(head,-,sizeof(head)); ecnt = ; }
Graph(){ init(); }
void addEdge(int u,int v,ll l)
{
fro[ecnt] = u;
to[ecnt] = v;
w[ecnt] = l;
nxt[ecnt] = head[u];
head[u] = ecnt++;
}
}G1,G2,G3;
int id[maxe]; const ll INF = 0x3f3f3f3f3f3f3f3fLL; int flag[maxe]; // 0 not shortest path 1 shortest path 2 unique shortest path
int dfs_clock,dfn[maxn],low[maxn];
void Tarjan(int u,int fa)
{
dfn[u] = low[u] = ++dfs_clock;
for(int i = G3.head[u]; ~i; i = G3.nxt[i]){
if((i^) == fa) continue; // == 优先级比^高
int v = G3.to[i];
if(!dfn[v]){
Tarjan(v,i);
low[u] = min(low[u],low[v]);
if(low[v]>dfn[u]) {
flag[id[i]] = ;
}
}else { low[u] = min(low[u],low[v]); }
}
}
struct Node
{
ll d;
int u;
bool operator < (const Node& x) const{
return d > x.d;
}
}; bool vis[maxn];
void Dijkstra(int s,Graph &g)
{
priority_queue<Node> q;
memset(g.d,0x3f,sizeof(g.d));
g.d[s] = ;
q.push(Node{,s});
while(q.size()){
Node x = q.top(); q.pop();
int u = x.u;
if(vis[u]) continue;
vis[u] = true;
for(int i = g.head[u]; ~i; i = g.nxt[i] ){
int v = g.to[i];
if(g.d[v]> g.d[u]+g.w[i]){
g.d[v] = g.d[u]+g.w[i];
q.push(Node{g.d[v],v});
}
}
}
memset(vis,,sizeof(vis));
} #define local
int main()
{
#ifdef local
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // local
int n,m,s,t;
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i = ; i < m; i++){
int u,v,l; scanf("%d%d%d",&u,&v,&l);
G1.addEdge(u,v,l); G2.addEdge(v,u,l);
}
Dijkstra(s,G1);
Dijkstra(t,G2);
ll shortest = G1.d[t];
for(int i = ; i < m; i++){
int u = G1.fro[i], v = G1.to[i];
if(shortest == G1.w[i] + G1.d[u] + G2.d[v] ){
id[G3.ecnt] = i; G3.addEdge(u,v,G1.w[i]);
id[G3.ecnt] = i; G3.addEdge(v,u,G1.w[i]);
flag[i] = ;
}
}
Tarjan(s,-); for(int i = ; i < m; i ++){
if(flag[i] == ) puts("YES");
else if(flag[i] == ){
if(G1.w[i]>) printf("CAN 1\n");
else puts("NO");
}else {
ll detal = G1.w[i] - shortest + G1.d[G1.fro[i]]+G2.d[G1.to[i]] + ;
if(G1.w[i] > detal)
printf("CAN %d\n",detal);
else puts("NO");
}
}
return ;
} /*
自以为是的优化:
q.push(s);
while(q.size()){
int u = q.front(); q.pop();
for(int i = G1.head[u]; ~i; i = G1.nxt[i]) if(!vis[i]) {
vis[i] = true;
int v = G1.to[i];
if(G1.d[u] + G1.w[i] == G1.d[v] && G2.d[v] + G1.w[i] == G2.d[u]){
id[G3.ecnt] = i; G3.addEdge(u,v,G1.w[i]);
id[G3.ecnt] = i; G3.addEdge(v,u,G1.w[i]);
flag[i] = 1;
q.push(v);
}
}
}
*/
Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )的更多相关文章
- Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...
- map Codeforces Round #Pi (Div. 2) C. Geometric Progression
题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...
- 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library
题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...
- Codeforces Round #Pi (Div. 2) ABCDEF已更新
A. Lineland Mail time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set乱搞
D. One-Dimensional Battle ShipsTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- Codeforces Round #Pi (Div. 2) C. Geometric Progression map
C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library set
B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #Pi (Div. 2) A. Lineland Mail 水
A. Lineland MailTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/proble ...
随机推荐
- Razor与HTML混合输出陷阱与技巧
3,Razor与HTML混合输出陷阱与技巧 属性名称误判 有时候我们必须让html内容与razor语句紧接在一起, eg: 您好,a先生 假设变量名称为ViewBag.Name, 您好, ...
- socket网络编程实践要点
1.创建udp的socket句柄 // 当host_port为0时,则表示让操作系统自动分配 bool createUdpSocket(string host_ip,unsigned short ho ...
- 我们团队是如何落地DDD的(1)
最近发现文章老是被窃取,有些平台举报了还没有用.请识别我的id方丈的寺院. 摘要 DDD领域驱动设计,起源于2004年著名建模专家Eric Evans发表的他最具影响力的著名书籍:Domain-Dri ...
- org.apache.commons.httpclient和org.apache.http.client区别(转)
官网说明: http://hc.apache.org/httpclient-3.x/ Commons HttpClient项目现已结束,不再开发.它已被其HttpClient和HttpCore模块中的 ...
- 洛谷P1868 饥饿的奶牛
P1868 饥饿的奶牛 题目描述 有一条奶牛冲出了围栏,来到了一处圣地(对于奶牛来说),上面用牛语写着一段文字. 现用汉语翻译为: 有N个区间,每个区间x,y表示提供的x~y共y-x+1堆优质牧草.你 ...
- 洛谷P2647 最大收益
P2647 最大收益 题目描述 现在你面前有n个物品,编号分别为1,2,3,……,n.你可以在这当中任意选择任意多个物品.其中第i个物品有两个属性Wi和Ri,当你选择了第i个物品后,你就可以获得Wi的 ...
- 洛谷P3959 宝藏(状压dp)
传送门 为什么感觉状压dp都好玄学……FlashHu大佬太强啦…… 设$f_{i,j}$表示当前选的点集为$i$,下一次要加入的点集为$j$时,新加入的点和原有的点之间的最小边权.具体的转移可以枚举$ ...
- IT兄弟连 Java语法教程 运行Java程序
编译好Java字节码文件后,接下来就应该运行Java程序了. 运行Java程序需要使用JDK中提供的java命令,因为已经把java命令所在的路径添加到了系统的Path环境变量中,因此现在可以直接使用 ...
- html常用标签及属性,常用英语单词
目录 html常用标签 html常用标签属性 前端常用英语单词表(未完待续) 欢迎评论点赞交流,转发请添加原博客连接谢谢! Ctrl键+F 可以查找你想要的内容哦! html常用标签 htmi结构 & ...
- EF升级 反射重载方法ApplyConfiguration
protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); //var ...