【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)
http://www.lydsy.com/JudgeOnline/problem.php?id=1631
看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd:那是因为你写的根本不是dij,,233
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005, M=100005, oo=~0u>>2;
int ihead[N], n, m, d[N], T, cnt, d1[N], X[M], Y[M], W[M];
struct ED { int to, next, w; }e[M];
struct ND { int id; const bool operator<(const ND &b) const { return d[id]>d[b.id]; } };
priority_queue<ND> q;
void add(int u, int v, int w) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
}
void dij(int s) {
for1(i, 0, n) d[i]=oo;
d[s]=0;
ND t={s};
int u, v;
q.push(t);
while(q.size()) {
u=q.top().id; q.pop();
for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]>d[u]+e[i].w) {
d[v]=d[u]+e[i].w;
t.id=v;
q.push(t);
}
}
} int main() {
read(n); read(m); read(T);
int u, v, w;
rep(i, m) {
read(u); read(v); read(w);
X[i]=u; Y[i]=v; W[i]=w;
add(v, u, w);
}
int mx=0;
dij(T);
for1(i, 1, n) d1[i]=d[i];
cnt=0; CC(ihead, 0);
rep(i, m) add(X[i], Y[i], W[i]);
dij(T);
for1(i, 1, n) {
mx=max(mx, d1[i]+d[i]);
}
print(mx);
return 0; }
Description
Input
第1行:三个用空格隔开的整数.
第2行到第M+1行,每行三个用空格隔开的整数:Ai, Bi,以及Ti.表示一条道路的起点,终点和需要花费的时间.
Output
唯一一行:一个整数: 所有参加聚会的奶牛中,需要花费总时间的最大值.
Sample Input
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
HINT
样例说明:
共有4只奶牛参加聚会,有8条路,聚会位于第2个农场.
第4只奶牛可以直接到聚会所在地(花费3时间),然后返程路线经过第1和第3个农场(花费7时间),总共10时间.
Source
【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)的更多相关文章
- 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...
- 【BZOJ】1697: [Usaco2007 Feb]Cow Sorting牛排序
[算法]数学置换 [题意]给定n个数,要求通过若干次交换两个数的操作得到排序后的状态,每次交换代价为两数之和,求最小代价. [题解] 考虑置换的定义:置换就是把n个数做一个全排列. 从原数组到排序数组 ...
- 【BZOJ】1697: [Usaco2007 Feb]Cow Sorting牛排序(置换群)
http://www.lydsy.com/JudgeOnline/problem.php?id=1697 置换群T_T_T_T_T_T_T 很久以前在黑书和白书都看过,,,但是看不懂... 然后找了本 ...
- 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)
http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...
- 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)
http://www.lydsy.com/JudgeOnline/problem.php?id=3301 其实这一题很早就a过了,但是那时候看题解写完也是似懂非懂的.... 听zyf神犇说是康托展开, ...
- 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...
- 【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2015 这种水题真没啥好说的.. #include <cstdio> #include & ...
- 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)
http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
随机推荐
- xmu 1125 越野车大赛(公式推导&&三分)(中等)
xmu 1125.越野车大赛 Description TheBeet正在參加一场越野车大赛.比赛的场地如右图:共分三块.每一块地面的长宽均为N与M,但地表情况不同.越野车在这段路面上的最快速度也不同. ...
- GridLayout with span
Widgets can span multiple columns or rows in a grid. In the next example we illustrate this. #!/usr/ ...
- UVA1614(贪心)
Hell on the Markets Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu [Subm ...
- 偏最小二乘回归(PLSR)- 1 概览
1. 概览 偏最小二乘算法,因其仅仅利用数据X和Y中部分信息(partial information)来建模,所以得此名字.其总体处理框架体现在下面两图中. 建议先看第2部分,对pls算法有初步了解后 ...
- Makefile之写demo时的通用Makefile写法
Makefile之写demo时的通用Makefile写法[日期:2013-05-22] 来源:CSDN 作者:gqb666 [字体:大 中 小] 前面的一篇文章Makefile之大型工程项目子目录M ...
- 最全的iOS物理引擎demo
概述 最全的iOS物理引擎demo,实现重力.碰撞.推力.摆动.碰撞+重力.重力弹跳.仿摩拜单车贴纸效果.防iMessage滚动效果.防百度外卖首页重力感应等效果! 详细 代码下载:http://ww ...
- Ubuntu下设置redis让其他服务器访问
修改redis配置文件,将 bind 127.0.0.1to bind 0.0.0.0Then restart your service (service redis-server restart) ...
- sql 百分比
select [city], bfb=cast(cast(count(*)*100./(select count(*) from [UserBasicInfo]) as decimal(10,0)) ...
- C# : 连接SDE空间数据库时出错
1.SDE认证问题:使用ArcGIS.KeyGen.exe生成一个epp文件方法:-->server-->All-->拷贝并保存为*.epp文件.认证时加载该文件即可认证完毕.2.实 ...
- sql中一列拆成两列
declare @table table (name nvarchar(4))insert into @tableselect '张三' union allselect '李四' union alls ...