【POJ】3268 Silver Cow Party
题目链接: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的更多相关文章
- (poj)3268 Silver Cow Party 最短路
Description One cow ≤ N ≤ ) conveniently numbered ..N ≤ X ≤ N). A total of M ( ≤ M ≤ ,) unidirection ...
- Dijkstra算法:POJ No 3268 Silver Cow Party
题目:http://poj.org/problem?id=3268 题解:使用 priority_queue队列对dijkstra算法进行优化 #include <iostream> #i ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- 【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, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【BZOJ3939】[Usaco2015 Feb]Cow Hopscotch 动态规划+线段树
[BZOJ3939][Usaco2015 Feb]Cow Hopscotch Description Just like humans enjoy playing the game of Hopsco ...
随机推荐
- 清理Visual Studio解决方案临时文件:Clean Visual Studio Solution Temporary File Build20160418
复制保存到任意文件名.bat,放置在Visual Studio Solution目录下. 当Visual Studio Solution目录过于庞大或打算拷贝移动Visual Studio Solut ...
- HIve分组查询返回每组的一条记录
select a.lng,a.lat from (select row_number() over ( partition by uid,grid_id) as rnum,weighted_centr ...
- git分布式版本控制系统权威指南学习笔记(四):git reset
文章目录 git reset目录树重写 git reset 重置 git reset目录树重写 git reset --soft 暂存区工作区不变 git reset --hard git reset ...
- bootstrap Modal 模态框垂直居中
解决 Modal 垂直居中的问题,上网找了好多博客,有好多说改源码的,这个并没有实践. 但发现另一种解决办法,可以实现,代码如下: function centerModals(){ $('.modal ...
- (转载) 深入理解ES6箭头函数的this以及各类this面试题总结
声明:本文转载自 https://blog.csdn.net/yangbingbinga/article/details/61424363 ES6中新增了箭头函数这种语法,箭头函数以其简洁性和方便获取 ...
- <python练习题>python练习题(常练勿忘)
学了python,去面试经常出现,某个或某些库不熟悉导则想不起来怎么写,知道思路而写不出来,多半还是不够熟悉,这里就作为熟悉python的地方,多做做题,多思考. 题目1:店铺ID为00000000- ...
- react-swiper 如何实现滑动小卡片的移动?
1.引入插件 import ReactSwipes from 'react-swipes'; import './swiperCard.css'; 2.代码构成 export default clas ...
- 转载:vs2010 问题 >LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
原文链接:http://www.cnblogs.com/newpanderking/articles/3372969.html >LINK : fatal error LNK1123: 转换到 ...
- 2019-7-15-WPF-测试触摸设备发送触摸按下和抬起不成对
title author date CreateTime categories WPF 测试触摸设备发送触摸按下和抬起不成对 lindexi 2019-7-15 9:3:51 +0800 2019-0 ...
- 笔记57 Mybatis和Hibernate的比较
一.Hibernate和MyBatis简介 1.Hibernate简介 Hibernate对数据库结构提供了较为完整的封装,Hibernate的O/R Mapping实现了POJO 和数据库表之间的映 ...