【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.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
随机推荐
- java反射--获取成员变量信息
获取成员变量信息 代码及说明: public static void printFieldMessage(Object obj) { //要获取类的信息,首先要获取类的类类型 Class c=obj. ...
- UML基本架构建模--类的辅助信息
Organizing Attributes and Operations 组织属性和操作 When drawing a class, you don't have to show every attr ...
- 【laravel54】composer install与composer update的区别
1.基础概念: 我们需要明白laravel项目里面有2个配置文件,composer.json和composer.lock文件,前者是下载的依赖包配置文件,后者是锁定的包版本信息. 使用之前,需要cd ...
- 微信公众平台自定义菜单及高级接口PHP SDK(转)
本文介绍介绍微信公众平台自定义菜单及高级接口的PHP SDK及使用方法. 作者 方倍工作室 修正记录: 2014.05.03 v1.0 方倍工作室 http://www.cnblogs.com/txw ...
- Linux命令-网络命令:wall
wall hello word 向所有登录用户发送消息hello world root用户自己也会收到消息,wangyunpeng用户收到消息如下图:
- linq to sql 去重复
ydc.GameScore.OrderByDescending(o => o.Score).GroupBy(ic => ic.UserPhone).Select(g => g.Fir ...
- Spring Cloud概述
Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...
- jQuery 插件分享-非常优秀的tab插件tabulous- 学徒帮
干货jquery插件分享之tab. tab 选项卡切换,在日常开发中也是一种比较常见的呈现控件,今天这个tab控件效果还是蛮喜欢的,推荐给大家有用到的场景可以试试: tabulous.js A jQu ...
- lzma解压
这个软件的使用方法有点特殊:需要将要压缩为lzma格式的文件拖放到批处理上面,会自动进行处理.压缩和解压同样是拖放到上面,程序会自动处理.程序默认使用2个CPU线程进行处理,会自动判断你是压缩还是解压 ...
- 绕过IE10直接安装VS2013
参考资料:http://blog.163.com/qimo601%40126/blog/static/1582209320143354446462/ 这SB设定我就懒得说了,安个IE10要安装N多WI ...