这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样...

先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer

-----------------------------------------------------------------------------

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#include<vector>
 
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define clr( x , c ) memset( x , c , sizeof( x ) )
 
using namespace std;
 
const int maxn = 1000 + 5;
const int inf = 0x7fffffff;
 
typedef pair< int , int > edge;
 
int cur = 0;
int n;
 
vector< edge > E[ 2 ][ maxn ];
  

void init() {

rep( i , n ) 
   E[ 0 ][ i ].clear() , E[ 1 ][ i ].clear();
}
 
inline void add( int x , int u , int v , int d ) {
E[ x ][ u ].push_back( make_pair( v , d ) );
}
 
inline void add_edge( int u , int v , int d ) {
add( 0 , u , v , d );
add( 1 , v , u , d );
}
 
bool inQ[ maxn ];
int d[ 2 ][ maxn ];
queue< int > Q;
 
void spfa( int S ) {
rep( i , n )
   d[ cur ][ i ] = inf;
d[ cur ][ S ] = 0;
   
clr( inQ , 0 );
while( ! Q.empty() ) Q.pop();
Q.push( S );
while( ! Q.empty() ) {
int x = Q.front();
Q.pop();
inQ[ x ] = false;
for( vector< edge > :: iterator e = E[ cur ][ x ].begin() ; e != E[ cur ][ x ].end() ; e++ ) {
int to = e -> first , dist = e -> second;
if( d[ cur ][ to ] > d[ cur ][ x ] + dist ) {
d[ cur ][ to ] = d[ cur ][ x ] + dist;
if( ! inQ[ to ] ) 
   inQ[ to ] = true , Q.push( to );
   
}
}
}
}
 
int main() {
// freopen( "test.in" , "r" , stdin );
init();
int m , s;
cin >> n >> m >> s;
--s;
while( m-- ) {
int u , v , d;
scanf( "%d%d%d" , &u , &v , &d );
u-- , v--;
add_edge( u , v , d );
}
spfa( s );
cur ^= 1;
spfa( s );
int ans = 0;
rep( i , n ) 
   ans = max( ans , d[ 0 ][ i ] + d[ 1 ][ i ] );
   
cout << ans << "\n";
return 0;
}

-----------------------------------------------------------------------------

1631: [Usaco2007 Feb]Cow Party

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 524  Solved: 388
[Submit][Status][Discuss]

Description

    农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前去X牛棚参加派对还是返回住所,她们都采用了用时最少的路线.那么,用时最多的奶牛需要多少时间来回呢?

Input

第1行:三个用空格隔开的整数.

第2行到第M+1行,每行三个用空格隔开的整数:Ai, Bi,以及Ti.表示一条道路的起点,终点和需要花费的时间.

Output

唯一一行:一个整数: 所有参加聚会的奶牛中,需要花费总时间的最大值.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

HINT

样例说明:

共有4只奶牛参加聚会,有8条路,聚会位于第2个农场.

第4只奶牛可以直接到聚会所在地(花费3时间),然后返程路线经过第1和第3个农场(花费7时间),总共10时间.

Source

BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )的更多相关文章

  1. BZOJ 1631: [Usaco2007 Feb]Cow Party

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

  2. bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】

    正反加边分别跑spfa最短路,把两次最短路的和求个max就是答案 #include<iostream> #include<cstdio> #include<queue&g ...

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

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

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

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

  5. 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 ...

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

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

  7. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)

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

  8. BZOJ1631: [Usaco2007 Feb]Cow Party

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 459  Solved: 338[Submit ...

  9. 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...

随机推荐

  1. 【转】几个常用的Oracle存储过程

    http://blog.bossma.cn/database/some-oracle-storing-process/ 几个常用的Oracle存储过程 发布时间:2008年1月6日 / 分类:Data ...

  2. Windows Server 2012 R2 Standard序列号

    备用一个吧,免得用起来的时候找不到. NB4WH-BBBYV-3MPPC-9RCMV-46XCB

  3. Bluetooth 2.1+EDR是什么

    目前应用最为广泛的是 Bluetooth 2.0+EDR标准,该标准在2004年已经推出,支持Bluetooth 2.0+EDR标准的产品也于2006年大量出现.虽然Bluetooth 2.0+EDR ...

  4. UVA122-Trees on the level(链二叉树)

    Trees on the level Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Sub ...

  5. Cocos2d-x3.0 捕Android菜单键和返回键

    原文地址:http://blog.csdn.net/qqmcy/article/details/26172665 .h void onKeyReleased(EventKeyboard::KeyCod ...

  6. linux之iptable

    转自:http://seanlook.com/2014/02/23/iptables-understand/ 一. netfilter与iptables Netfilter是由Rusty Russel ...

  7. 文件上传下载样式 --- bootstrap

    在平时工作中,文件上传下载功能属于不可或缺的一部分.bootstrap前端样式框架也使用的比较多,现在根据bootstrap强大的样式模板,自定义一种文件下载的样式. 后续会使用spring MVC框 ...

  8. input file 模拟

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. ie6与固定定位fixed,+ 条件注释格式注意

    ie6并不支持position:fixed, ie7+都支持fixed定位, ie6固定定位实现方法1: <!DOCTYPE html> <html> <head> ...

  10. SIGAR - System Information Gatherer And Reporter

    https://support.hyperic.com/display/SIGAR/Home 收藏一篇: http://www.cnitblog.com/houcy/archive/2012/11/2 ...