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. Autodesk FBX SDK Program 中文 (一)

    这是我的FBX SDK学习笔记.如文有错误.麻烦各位大大指出 为什么要使用FBX SDK? 由于3D建模软件都被AutoDesk收购了.FBX能够在各个建模软件之间互相导入导出,在非常多游戏引擎中也用 ...

  2. JDBC 可更新和对更新敏感的结果集

    public class OtherApi {     /**      * @param args      * @throws SQLException      * @throws Interr ...

  3. Maven + Apache Felix + CXF + DOSGi series

    This is a blog series on how to combine Maven + Apache Felix + CXF + DOSGi. The information presente ...

  4. 深入单例模式 - Java实现

    单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好.用对单例模式,还真得费一番脑筋.本文对Java中常见的单例模式写法做了一个总结,如有错漏之处,恳请读者指正. 饿汉法 顾名思义,饿汉法 ...

  5. index.js

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  6. 阿里云rds linux平台使用wget 工具下载备份与日志文件

    1. 获取备份下载地址 RDS 控制台  备份恢复  数据备份,选择需要下载的备份集,点击“下载”. 点击“复制内网地址” 或 “复制外网地址” 来获取备份的 内网 或 外网 下载地址. 日志备份的地 ...

  7. XML相关转换

    1.将DataTable转换成xml字符串 //将DataTable转换成xml字符串: public string ConvertDataTableToXml(DataTable dt) { Mem ...

  8. C#位操作

    一.原码与补码 在计算机系统中,数值一律用补码来存储(表示).主要原因:使用补码,可以将符号位和其他位统一处理:同时减法也可按加法来处理.另外,两个补码表示的数相加时,如果最高位(符号位)有进位,则进 ...

  9. struts2实现简单文件上传

    struts2 在内部已经帮我们做了很多封装的工作,我们只需要简单的写一些配置即可. 1 表单页面 <form action="${pageContext.request.contex ...

  10. [Jobdu] 题目1377:缓变序列

    题目描述: 陈博在写论文时碰到一个难题:如何将给定的整数序列变换成缓变序列:即任意两个相邻的元素相差均为1,第1个元素和最后一个元素相差也为1. 变换是指改变原整数序列中各元素的顺序.例如整数序列1, ...