正反加边分别跑spfa最短路,把两次最短路的和求个max就是答案

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int N=2005,M=200005;
int n,m,s,x[M],y[M],z[M],h[N],cnt,c[N],d[N];
bool v[N];
struct qwe
{
int ne,to,va;
}e[M];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
void spfa(int *dis)
{
deque<int>q;
for(int i=1;i<=n;i++)
dis[i]=1e9;
dis[s]=0,v[s]=1,q.push_front(s);
while(!q.empty())
{
int u=q.front();
q.pop_front();
v[u]=0;
for(int i=h[u];i;i=e[i].ne)
if(dis[e[i].to]>dis[u]+e[i].va)
{
dis[e[i].to]=dis[u]+e[i].va;
if(!v[e[i].to])
{
v[e[i].to]=1;
if(q.empty()||dis[q.front()]<dis[e[i].to])
q.push_back(e[i].to);
else
q.push_front(e[i].to);
}
}
}
}
int main()
{
n=read(),m=read(),s=read();
for(int i=1;i<=m;i++)
{
x[i]=read(),y[i]=read(),z[i]=read();
add(x[i],y[i],z[i]);
}
spfa(c);
memset(h,0,sizeof(h));
cnt=0;
for(int i=1;i<=m;i++)
add(y[i],x[i],z[i]);
spfa(d);
int ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,c[i]+d[i]);
printf("%d\n",ans);
return 0;
}

bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】的更多相关文章

  1. BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )

    这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer --------------------- ...

  2. BZOJ 1631: [Usaco2007 Feb]Cow Party

    题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 491  Solved: 362[Sub ...

  3. bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】

    因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<q ...

  4. bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】

    直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...

  5. 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd: ...

  6. BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...

  7. bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】

    至今都不知道置换群是个什么东西--题解说什么就是什么.jpg 以下来自hzwer:http://hzwer.com/3905.html #include<iostream> #includ ...

  8. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序

    Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都是一个 ...

  9. bzoj 1119 [POI2009]SLO && bzoj 1697 [Usaco2007 Feb]Cow Sorting牛排序——思路(置换)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...

随机推荐

  1. 【01】emmet系列之基础介绍

    [01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 相关网址 官网:http: ...

  2. AndroidSweetSheet:ViewPager的实现(2)

     AndroidSweetSheet:ViewPager的实现(2) 附录文章9说明了AndroidSweetSheet典型的列表样式实现,本文写一个例子,说明AndroidSweetSheet以 ...

  3. HDU 4416 (后缀自动机)

    HDU 4416 Good Article Good sentence Problem : 给一个串S,和一些串T,询问S中有多少个子串没有在T中出现. Solution :首先对所有的T串建立后缀自 ...

  4. jquery判断单选按钮radio是否选中的方法

    JQuery控制radio选中和不选中方法总结 一.设置选中方法 复制代码代码如下: $("input[name='名字']").get(0).checked=true; $(&q ...

  5. HDU 5950 Recursive sequence 递推转矩阵

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. [bzoj1978][BeiJing2010]取数游戏 game_动态规划_质因数分解

    取数游戏 game bzoj-1978 BeiJing-2010 题目大意:给定一个$n$个数的$a$序列,要求取出$k$个数.假设目前取出的数是$a_j$,那么下次取出的$a_k$必须保证:$j&l ...

  7. Ubuntu 16.04安装Ubuntu After Install工具实现常用软件批量安装

    这个软件集成了常用且好用的软件,且只需要选择需要的软件之后自动安装好,不需要额外设置. 安装: sudo add-apt-repository ppa:thefanclub/ubuntu-after- ...

  8. MongoDB使用教程收集(语法教程)

    https://www.tutorialspoint.com/mongodb/index.htm https://wizardforcel.gitbooks.io/w3school-mongodb/c ...

  9. 加上mvc:resources后controller访问404

    之前因为静态资源访问,404,于是加上了类似的代码 <mvc:resources location="/resources/" mapping="/resource ...

  10. android中单元測试中的断言assert的使用与扩展

    首先看一组对照,比方说我们要測试的结果是一个Linearlaout AssertJ Android: assertThat(layout).isVisible() .isVertical() .has ...