bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】
正反加边分别跑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】的更多相关文章
- BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )
这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer --------------------- ...
- BZOJ 1631: [Usaco2007 Feb]Cow Party
题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 491 Solved: 362[Sub ...
- bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】
因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<q ...
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】
直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...
- 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)
http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd: ...
- BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...
- bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】
至今都不知道置换群是个什么东西--题解说什么就是什么.jpg 以下来自hzwer:http://hzwer.com/3905.html #include<iostream> #includ ...
- BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序
Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都是一个 ...
- 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 ...
随机推荐
- 【01】《响应式Web设计:HTML5和CSS3实战》
[01] (魔芋:已看完.) [01]<响应式Web设计:HTML5和CSS3实战>(全).pdf 共246页. 2013年1月出版. 读后感:适合入门的书籍,对于响应式布局, ...
- 【Objective-C】07-自定义构造方法和description方法
在第5讲中已经介绍了如何定义类和创建并初始化对象,比如有Student这个类 1.Student.h 1 #import <Foundation/Foundation.h> 2 3 @in ...
- 华中农业大学第四届程序设计大赛网络同步赛-1020: Arithmetic Sequence,题挺好的,考思路;
1020: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MB Submit: ->打开链接<- Descriptio ...
- [BZOJ3751][NOIP2014]解方程(数学相关+乱搞)
题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...
- 使用谷歌Z生成条形码以及二维码
下载地址:http://zxingnet.codeplex.com/ zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 首先下载二进制dll文件,引入工程: using Sy ...
- 2018/2/17 SpringCloud的一个简单小介绍
在学习SpringCloud之前,我以为SpringCloud是与Double一样,只是个单纯的RPC框架.但在今天的学习中,我发现并非如此,事实上,SpringCloud是多个框架的集合,感觉Spr ...
- 【Tomcat】tomcat启动后查看运行时JVM参数
Tomcat优化配置参考http://www.cnblogs.com/qlqwjy/p/8007490.html 1.启动服务后访问localhost,点击Server Status
- Intent使用Parcelable传递对象
package com.pingyijinren.test; import android.os.Parcel; import android.os.Parcelable; import java.i ...
- HTTPS 是如何保证安全的?
每当我们讨论到信息安全的时候,我们最长接触到的信息加密传输的方式莫过于 HTTPS 了,当我们浏览器地址栏闪现出绿色时,就代表着这个网站支持 HTTPS 的加密信息传输方式,并且你与它的连接确实被加密 ...
- ssh forwarding 配置
假设有服务器A,属于某一内网,无法直接登录. A: 10.0.1.48 root/password@a 但我们有一台跳板机器可以访问该Server A B: 10.0.2.48 root/passwo ...