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

    农场有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(dijkstra)的更多相关文章

  1. 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...

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

    [算法]数学置换 [题意]给定n个数,要求通过若干次交换两个数的操作得到排序后的状态,每次交换代价为两数之和,求最小代价. [题解] 考虑置换的定义:置换就是把n个数做一个全排列. 从原数组到排序数组 ...

  3. 【BZOJ】1697: [Usaco2007 Feb]Cow Sorting牛排序(置换群)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1697 置换群T_T_T_T_T_T_T 很久以前在黑书和白书都看过,,,但是看不懂... 然后找了本 ...

  4. 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...

  5. 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3301 其实这一题很早就a过了,但是那时候看题解写完也是似懂非懂的.... 听zyf神犇说是康托展开, ...

  6. 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...

  7. 【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2015 这种水题真没啥好说的.. #include <cstdio> #include & ...

  8. 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...

  9. 【BZOJ】3053: The Closest M Points(kdtree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...

随机推荐

  1. 转载【linux】Linux学习之CentOS6.x+7.x---网卡的配置

    转载至:http://www.cnblogs.com/smyhvae/p/3932903.html [正文] Linux系统版本:Centos 6.5 Linux系统版本:Centos 7 目的:将c ...

  2. 总结一些js自定义的函数

    1.dayin() 作用:将id为dayin的内容,新建页面并打印,可解决打印某页面中的部分内容的问题. 使用方法:将要打印的内容通过 <span id="dayin"> ...

  3. “cvc-complex-type.2.4.a: Invalid content was found starting with element 'taglib'”错误的解决办法

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" ...

  4. FFmpeg + SDL2 实现的视频播放器「视音频同步」

    文章转自:http://blog.csdn.net/i_scream_/article/details/52760033 日期:2016.10.8 作者:isshe github:github.com ...

  5. c#中用DirectShow实现媒体播放器

    原文地址:https://www.cnblogs.com/aiqingqing/p/4338448.html 用.net做多媒体开发的似乎不多,所以网上资源也少,看的人更少.不过我的博客上居然还有几位 ...

  6. 搜狐畅游CEO王滔辞职

    凤凰科技讯 11月3日消息,搜狐公布公告确认搜狐畅游CEO离职.公告称王滔因个人原因辞去畅游首席运行官职务.将继续担任畅游公司董事和首席产品官. 据搜狐公告,董事会任命搜狐总裁余楚媛与畅游总裁陈德文为 ...

  7. 百度MIP(百度版的google AMP)了解一下?

    官网:https://www.mipengine.org/ 视频教学:http://bit.baidu.com/subject/datalist/sid/10/cid/22.html github:h ...

  8. Mysql 数据库数值类型详解

    MySQL 支持所有标准SQL 中的数值类型,其中包括严格数值类型(INTEGER.SMALLINT.DECIMAL 和NUMERIC),以及近似数值数据类型(FLOAT.REAL 和DOUBLE P ...

  9. Javascript中的对象和原型(一)(转载)

    面向对象的语言(如Java)中有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是,JavaScript 没有类的概念,因此它的对象也与基于类的语言中的对象有所不同. 要了解面向对象,首 ...

  10. Hadoop知识汇总

    Hadoop的两大功能:海量数据存储和海量数据分析 Hadoop2的三大核心组件是:HDFS.MapperReducer和yarn 1.HDFS:分布式文件系统海量数据存储 2.MapperReduc ...