题目链接:http://poj.org/problem?id=3268

题意 :有N头奶牛,M条单向路。X奶牛开party,其他奶牛要去它那里。每头奶牛去完X那里还要返回。去回都是走的最短路。现在问这里面哪头奶牛走的路最长。

题解:对每个奶牛i与X做两次spfa。去回各一次。然后统计最长的。。板子稍微改一改//但是我还是T了好几发,因为初始化数组的时候maxn开大了。。QAQ。改小了就过了。

代码:

 #include<iostream>
#include<stack>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = ; vector< pair<int,int> > e[maxn]; int n,m,X;
int d[maxn],inq[maxn]; int spfa(int s,int t){
for(int i = ;i < maxn ; i++)
inq[i] = ;
for(int i = ; i < maxn ; i++)
d[i] = 1e9;
queue<int>Q;
Q.push(s);d[s] = ;inq[s] = ;
while( !Q.empty() ){
int now = Q.front();
Q.pop();
inq[now] = ;
for(int i = ; i < e[now].size() ; i++){
int v = e[now][i].first;
if(d[v] > d[now] + e[now][i].second){
d[v] = d[now] + e[now][i].second;
if(inq[v] == )
continue;
inq[v] = ;
Q.push(v);
}
}
}
return d[t];
} int main() {
scanf("%d%d%d",&n,&m,&X);
int x,y,z;
for(int i = ; i < m ;i++){
scanf("%d%d%d",&x,&y,&z);
//cin>>x>>y>>z;
e[x].push_back(make_pair(y,z));
//e[y].push_back(make_pair(x,z));
} int ans = ;
for(int i = ;i <= n ;i++){
if(i == X){
continue;
}
int sum = spfa(X,i);
sum += spfa(i,X);
ans = max(ans,sum);
}
cout<<ans<<endl; return ;
}

【POJ】3268 Silver Cow Party的更多相关文章

  1. (poj)3268 Silver Cow Party 最短路

    Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...

  2. Dijkstra算法:POJ No 3268 Silver Cow Party

    题目:http://poj.org/problem?id=3268 题解:使用 priority_queue队列对dijkstra算法进行优化 #include <iostream> #i ...

  3. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  4. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  5. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  6. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  7. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  8. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  9. 【BZOJ3939】[Usaco2015 Feb]Cow Hopscotch 动态规划+线段树

    [BZOJ3939][Usaco2015 Feb]Cow Hopscotch Description Just like humans enjoy playing the game of Hopsco ...

随机推荐

  1. (53)C# 工具

    https://docs.microsoft.com/zh-cn/dotnet/framework/tools/ildasm-exe-il-disassembler 一.Visual Studio的开 ...

  2. (10)centos7 包管理、远程传文件

    一.RPM red package manager 红帽包管理工具 -q 查询 -a 已安装的所有rpm 1.查询已安装的rpm列表 -qa 查看所有的rpm安装包 rpm -qa | grep py ...

  3. hibernate使用手写sql以及对结果list的处理

    Session sees=simpleDAO.getSessionFactory().openSession(); String sql = "select * from fhcb_08_t ...

  4. java发带图片正文和附件的邮件mail

    package com.mail; import java.io.UnsupportedEncodingException; import java.util.Date; import java.ut ...

  5. C++——指针与数组

    1.数组名不是指针,神似指针(可以将数组名直接赋值给指针) (1)数组名的内涵在于其指代实体是一种数据结构,这种数据结构就是数组:(2)数组名的外延在于其可以转换为指向其指代实体的指针,而且是一个指针 ...

  6. 错误 1 error C4996: 'getcwd': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getcwd. See online help for details.

    解决办法: 属性>C/C++>预处理定义>编辑>添加_CRT_NONSTDC_NO_DEPRECATE>应用

  7. Spring Boot跨域问题解决方案

    @Configurationpublic class CorsConfig { @Bean public FilterRegistrationBean corsFilter() { UrlBasedC ...

  8. keepalived高可用haproxy负载均衡varnish缓存wordpress的动静分离(第一次配置成功)

    haproxy和nginx都可以作为七层和四层反代服务器对外提供服务,此文通过haproxy和keealived配置varnish搭建wordpress的动静分离站点 一.实验环境 五台虚拟机: ha ...

  9. leetcode.数组.287寻找重复数-Java

    1. 具体题目 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: ...

  10. mysql 查询正在执行的sql

    select * from information_schema.`PROCESSLIST` where info is not null; 或者 -- use information_schema; ...