题目链接:http://poj.org/problem?id=3268

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19211   Accepted: 8765

Description

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?

Input

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

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
题目大意:(单向路)有n头牛,他们商量好去x家举行party,需要你帮他们计算出其他人去到x家party结束后回到自己家所需要的最短路程,然后输出最长路程。(每头牛去的路程可能和回家的路程不同)
解题思路:用两遍dijkstra 分别计算出其他牛到x家的最短路径,然后再计算出他们从x家返回到自己家的最短路程,最后找出最长路径
 #include <stdio.h>
#include <string.h>
#define inf 9999999999
int p1[][];
int p2[][];
int vis1[];
int vis2[];
int dis1[];
int dis2[];
int n,m,x;
void dijkstra_go() //计算从自己家到x家所需要的最短路径(注意用的p2数组)反向思维
{
int i,j,pos = ,minn;
for (i = ; i <= n; i ++)
{
vis1[i] = ;
dis1[i] = p2[x][i];
}
vis1[x] = ;
dis1[x] = ; for (i = ; i <= n; i ++)
{
minn = inf;
for (j = ; j <= n; j ++)
{
if (!vis1[j] && dis1[j] < minn)
{
minn = dis1[j];
pos = j;
}
}
vis1[pos] = ;
for (j = ; j <= n; j ++)
{
if (!vis1[j] && dis1[j] > dis1[pos]+p2[pos][j])
dis1[j] = dis1[pos]+p2[pos][j];
}
}
}
void dijkstra_back() //计算从x家回到自己家所需要的最短路径
{
int i,j,pos = ,minn;
for (i = ; i <= n; i ++)
{
vis2[i] = ;
dis2[i] = p1[x][i];
}
vis2[x] = ;
dis2[x] = ;
for (i = ; i <= n; i ++)
{
minn = inf;
for (j = ; j <= n; j ++)
{
if (!vis2[j] && dis2[j] < minn)
{
minn = dis2[j];
pos = j;
}
}
vis2[pos] = ;
for (j = ; j <= n; j ++)
{
if (!vis2[j] && dis2[j] > dis2[pos]+p1[pos][j])
dis2[j] = dis2[pos]+p1[pos][j];
}
}
}
int main ()
{
int a,b,t;
int i,j;
int sum[];
while (~scanf("%d%d%d",&n,&m,&x))
{
for (i = ; i <= n; i ++)
{
for (j = ; j <= n; j ++)
{
p1[i][j] = inf;
p2[i][j] = inf;
}
} for (i = ; i < m; i ++)
{
scanf("%d%d%d",&a,&b,&t);
p1[a][b] = t;
p2[b][a] = t;
}
dijkstra_go();
dijkstra_back();
int maxx = ;
for (i = ; i <= n; i ++)
{
if (i == x)
continue;
sum[i] = dis1[i]+dis2[i];
if (maxx < sum[i])
maxx = sum[i];
}
printf("%d\n",maxx);
}
return ;
}
												

POJ 3268 Silver Cow Party (双向dijkstra)的更多相关文章

  1. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:28457   Accepted: 12928 ...

  2. (简单) POJ 3268 Silver Cow Party,Dijkstra。

    Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...

  3. POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)

    题目链接: https://vjudge.net/problem/POJ-3268 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently n ...

  4. POJ 3268 Silver Cow Party ( Dijkstra )

    题目大意: 有N个农场每个农场要有一头牛去参加一个聚会,连接每个农场有m条路, 聚会地点是X,并且路是单向的.要求的是所有牛赶到聚会地点并且回到自己原先的农场所需要的最短时间. 题目分析: 其实就是以 ...

  5. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  6. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  7. POJ 3268 Silver Cow Party 最短路

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

  8. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

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

  9. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

随机推荐

  1. superobject中 JavaToDelphiDateTime的使用

    procedure TForm1.FormCreate(Sender: TObject); var n: TDateTime; i64: Int64; s: Integer; begin Memo1. ...

  2. Rhel7的基本使用

    1.修改主机名 [root@localhost ~]# cat /etc/hostname localhost.localdomain[root@localhost ~]# hostnamectl s ...

  3. [转]source inslght使用指导

    作为一个开放源代码的操作系统,Linux附带的源代码库使得广大爱好者有了一个广泛学习.深入钻研的机会,特别是Linux内核的组织极为复杂,同时,又不能像windows平台的程序一样,可以使用集成开发环 ...

  4. PHP及Javascript 正则判断中文(转)

    UTF-8匹配: 在javascript中,要判断字符串是中文是很简单的.比如: var str = "php编程"; if (/^[\u4e00-\u9fa5]+$/.test( ...

  5. pig

    1.Pig是基于hadoop的一个数据处理的框架. MapReduce是使用java进行开发的,Pig有一套自己的数据处理语言,Pig的数据处理过程要转化为MR来运行.2.Pig的数据处理语言是数据流 ...

  6. Thread IsBcakgroud

    C#中,Thread类有一个IsBackground 的属性.MSDN上对它的解释是:获取或设置一个值,该值指示某个线程是否为后台线程.个人感觉这样的解释等于没有解释. .Net中的线程,可以分为后台 ...

  7. CSS练习一(模仿163邮箱登陆)

    // '); code = code.replace(/&/g, '&'); return code; }; var runCode = function (code) { if (c ...

  8. PAT 07-2 A+B和C

    有两个值得注意的地方:1.变长数组(VLA)的使用,没想到PAT上的OJ竟然支持C99,一开始不知道就没用,看了看别人的,既然,还是用吧, 它有一点我不太喜欢,它不能像一般数组那样在声明时通过赋一个0 ...

  9. 利用strut2标签自动生成form前端验证代码

    利用strut2标签自动生成form前端验证代码,使用到的技术有1.struts2标签,如<s:form> <s:textfieled>2.struts2读取*Validati ...

  10. struts2和servlet同时用(访问servlet时被struts2过滤器拦截问题的解决)

    在同一个项目中间,如果既用到servlet有用了struts2的框架,运行项目时可能无法正常使用servlet,原因是在配置struts2的核心控制器时<url-pattern>/*< ...