题目描述

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。

每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。

输入输出格式

输入格式:

第一行三个整数N,M, X;

第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。

输出格式:

一个整数,表示最长的最短路得长度。

输入输出样例

输入样例#1:

  1. 4 8 2
  2. 1 2 4
  3. 1 3 2
  4. 1 4 7
  5. 2 1 1
  6. 2 3 5
  7. 3 1 2
  8. 3 4 4
  9. 4 2 3
输出样例#1:

  1. 10

说明

可以算得上是最短路的比较模板的题目了。

首先理解一下题意,既然要算来回的路径距离,当然要求两遍最短路了,反正我是想不出更好的办法了。

来回的距离那自然不难想出,正着建边后,再反向建边。

我们需要在第一次建边的时候用数组将遍的两点记录下来,方便下次建边。

然后跑两遍最短路spfa就好啦。

代码:

  1. /*
  2. Name: luogu 1821 Silver Cow Party
  3. Author: Manjusaka
  4. Date: 18-07-14 14:25
  5. */
  6.  
  7. #include <iostream>
  8. #include <cstring>
  9. #include <cstdio>
  10. #include <queue>
  11. using namespace std;
  12. #define N int(1e3+2)
  13. #define M int(1e5+2)
  14. int a[M],b[M],c[M];
  15. int n,m,s,ans;
  16. int head[N],tot;
  17. struct ahah{
  18. int nxt,to,dis;
  19. }edge[M];
  20. int d[N],dd[N];
  21. void add(int x,int y,int z)
  22. {
  23. edge[++tot].nxt=head[x],edge[tot].to=y,edge[tot].dis=z,head[x]=tot;
  24. }
  25. bool vis[N];
  26. queue <int> que;
  27. void spfa(int s)
  28. {
  29. for(int i=;i<=n;i++)d[i]=0x7fffff;
  30. vis[s]=;que.push(s);d[s]=;
  31. while(!que.empty())
  32. {
  33. int temp=que.front();
  34. vis[temp]=; que.pop();
  35. for(int i=head[temp];i;i=edge[i].nxt)
  36. {
  37. int v=edge[i].to;
  38. if(d[v]>d[temp]+edge[i].dis)
  39. {
  40. d[v]=d[temp]+edge[i].dis;
  41. if(!vis[v])
  42. {
  43. vis[v]=;
  44. que.push(v);
  45. }
  46. }
  47. }
  48. }
  49. }
  50. int main()
  51. {
  52. scanf("%d%d%d",&n,&m,&s);
  53. for(int i=;i<=m;i++)
  54. {
  55. scanf("%d%d%d",&a[i],&b[i],&c[i]);
  56. add(a[i],b[i],c[i]);
  57. }
  58. spfa(s);
  59. for(int i=;i<=n;i++)dd[i]=d[i];
  60. tot=;
  61. memset(vis,,sizeof(vis));
  62. memset(head,,sizeof(head));
  63. for(int i=;i<=m;i++)add(b[i],a[i],c[i]);
  64. spfa(s);
  65. for(int i=;i<=n;i++)
  66. {
  67. if(d[i]+dd[i]>ans)ans=d[i]+dd[i];
  68. }
  69. printf("%d",ans);
  70. }

luogu P1821 Silver Cow Party的更多相关文章

  1. 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  2. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  3. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 ...

  4. Silver Cow Party(最短路,好题)

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  5. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  6. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  7. poj 3268 Silver Cow Party

                                                                                                       S ...

  8. POJ 3268 Silver Cow Party (最短路dijkstra)

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

  9. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

随机推荐

  1. codeforces 611C

    题意: 给你一个矩阵,矩阵里有" . "和" # "," . "表示空的," # "表示禁止的. 多米诺骨牌将占据正好有 ...

  2. bzoj 1488: [HNOI2009]图的同构【polya定理+dfs】

    把连边和不连边看成黑白染色,然后就变成了 https://www.cnblogs.com/lokiii/p/10055629.html 这篇讲得好!https://blog.csdn.net/wzq_ ...

  3. bzoj 1396: 识别子串【SAM+线段树】

    建个SAM,符合要求的串显然是|right|==1的节点多代表的串,设si[i]为right集合大小,p[i]为right最大的r点,这些都可以建出SAM后再parent树上求得 然后对弈si[i]= ...

  4. poj 1182 食物链【带权并查集】

    设相等的边权为0,吃的边权为,被吃的边权为2,然后用带权并查集在%3的意义下做加法即可 关系为简单环的基本都可以用模环长的方式是用带权并查集 #include<iostream> #inc ...

  5. oauth2(spring security)报错method_not_allowed(Request method 'GET' not supported)解决方法

    报错信息 <MethodNotAllowed> <error>method_not_allowed</error> <error_description> ...

  6. Hue的全局配置文件hue.ini(图文详解)

    Hue版本:hue-3.9.0-cdh5.5.4 需要编译才能使用(联网) 说给大家的话:大家电脑的配置好的话,一定要安装cloudera manager.毕竟是一家人的.同时,我也亲身经历过,会有部 ...

  7. HDU - 6058 Kanade's sum

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...

  8. 转 做了两款数据库监控工具(mysql and nosql),打算在近期开源

    http://www.cnblogs.com/leefreeman/p/7297549.html 监控指标:https://www.linuxidc.com/Linux/2015-08/122009. ...

  9. c库函数-字符串

    一 strok:从字符串中按照分隔符提取所有字串 char s[] = "水发产品,47.6,不合格,mg/kg,17-05-21 15:04;";  char *delim = ...

  10. session共享方法

    session数据保存在memcached,redis这种内存数据库内 memcache比较简单,使用的场景比较多.redis支持的数据类型多.而且数据具有期限,和session和期限可以配合使用 通 ...