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

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

因为迪杰斯特拉求得是一点到其他所有点的最短路径,所以先把起点设成X,跑一次之后算出了每头牛回去的最短路径,然后把边反转,入边变成出边,出边变成入边,然后再把起点设成X,跑一次之后就算出了每头牛来的最短路径,最后把每头牛来回的最短路径加起来,取所有牛的最大值。写得太挫,明天补发其他解法。
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; const int SIZE = ;
const int INF = 0x6fffffff;
int MAP[SIZE][SIZE];
bool VIS[SIZE][SIZE];
int BOX[SIZE];
int D[SIZE],S[SIZE];
int N,M,X;
struct Comp
{
bool operator ()(int & a,int & b)
{
return D[a] > D[b];
}
};
struct Node
{
int vec,cost;
};
vector<Node> G[SIZE]; void dijkstra(int);
int main(void)
{
Node temp;
int from; while(scanf("%d%d%d",&N,&M,&X) != EOF)
{
fill(&MAP[][],&MAP[SIZE - ][SIZE - ],INF);
fill(&VIS[][],&VIS[SIZE - ][SIZE - ],false);
fill(BOX,BOX + SIZE - ,);
for(int i = ;i <= N;i ++)
G[i].clear();
for(int i = ;i < M;i ++)
{
scanf("%d%d%d",&from,&temp.vec,&temp.cost);
G[from].push_back(temp);
MAP[from][temp.vec] = temp.cost;
}
dijkstra(X);
for(int i = ;i <= N;i ++)
BOX[i] = D[i]; for(int i = ;i <= N;i ++)
G[i].clear();
for(int i = ;i <= N;i ++)
for(int j = ;j <= N;j ++)
{
temp.vec = j;
temp.cost = MAP[j][i];
G[i].push_back(temp);
}
dijkstra(X);
int ans = -;
for(int i = ;i <= N;i ++)
{
BOX[i] += D[i];
ans = ans > BOX[i] ? ans : BOX[i];
}
printf("%d\n",ans);
} return ;
} void dijkstra(int s)
{
fill(D,D + SIZE,INF);
fill(S,S + SIZE,false);
priority_queue<int,vector<int>,Comp> que;
D[s] = ;
que.push(s); while(!que.empty())
{
int cur = que.top();
que.pop();
S[cur] = true; for(int i = ;i < G[cur].size();i ++)
if(!S[G[cur][i].vec] && D[G[cur][i].vec] > D[cur] + G[cur][i].cost)
{
D[G[cur][i].vec] = D[cur] + G[cur][i].cost;
que.push(G[cur][i].vec);
}
}
}

补发贝尔曼和SPFA

 #include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std; const int INF = 0x6fffffff;
const int SIZE = ;
int N,M,X;
int MAP[SIZE][SIZE],D[SIZE],BOX[SIZE];
bool VIS[SIZE];
struct Node
{
int vec,cost;
};
vector<Node> G[SIZE]; void SPFA(int);
bool relax(int,int,int);
int main(void)
{
int from,to,cost,ans;
Node temp; while(scanf("%d%d%d",&N,&M,&X) != EOF)
{
fill(BOX,BOX + SIZE,);
fill(&MAP[][],&MAP[SIZE - ][SIZE - ],INF); for(int i = ;i < M;i ++)
{
scanf("%d%d%d",&from,&to,&cost);
MAP[from][to] = cost;
temp.vec = to;
temp.cost = cost;
G[from].push_back(temp);
}
SPFA(X);
for(int i = ;i <= N;i ++)
BOX[i] += D[i];
for(int i = ;i <= N;i ++)
{
G[i].clear();
for(int j = ;j <= N;j ++)
{
if(MAP[j][i] == INF)
continue;
temp.vec = j;
temp.cost = MAP[j][i];
G[i].push_back(temp);
}
}
SPFA(X);
ans = -;
for(int i = ;i <= N;i ++)
{
BOX[i] += D[i];
ans = ans > BOX[i] ? ans : BOX[i];
}
printf("%d\n",ans);
} return ;
} void SPFA(int s)
{
fill(D,D + SIZE,INF);
fill(VIS,VIS + SIZE,false);
D[s] = ;
queue<int> que;
que.push(s);
VIS[s] = true; while(!que.empty())
{
int cur = que.front();
que.pop();
VIS[cur] = false; for(int i = ;i < G[cur].size();i ++)
{
if(relax(cur,G[cur][i].vec,G[cur][i].cost))
{
que.push(G[cur][i].vec);
VIS[G[cur][i].vec] = true;
}
}
}
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}

Bellman_ford

#include <iostream>
#include <cstdio>
using namespace std; const int INF = 0x6fffffff;
const int SIZE = ;
int D[SIZE],BOX[SIZE];;
int N,M,X;
struct Node
{
int from,to,cost;
}G[SIZE * ]; void Bellman_ford(int);
bool relax(int,int,int);
int main(void)
{
int ans; while(scanf("%d%d%d",&N,&M,&X) != EOF)
{
fill(BOX,BOX + SIZE,);
for(int i = ;i < M;i ++)
scanf("%d%d%d",&G[i].from,&G[i].to,&G[i].cost);
Bellman_ford(X);
for(int i = ;i <= N;i ++)
BOX[i] += D[i];
for(int i = ;i < M;i ++)
swap(G[i].from,G[i].to);
Bellman_ford(X);
ans = -;
for(int i = ;i <= N;i ++)
{
BOX[i] += D[i];
ans = ans > BOX[i] ? ans : BOX[i];
}
printf("%d\n",ans);
} return ;
} void Bellman_ford(int x)
{
fill(D,D + SIZE,INF);
D[x] = ; bool flag;
for(int i = ;i < N - ;i ++)
{
flag = false;
for(int j = ;j < M;j ++)
if(relax(G[j].from,G[j].to,G[j].cost))
flag = true;
if(!flag)
break;
}
} bool relax(int from,int to,int cost)
{
if(D[to] > D[from] + cost)
{
D[to] = D[from] + cost;
return true;
}
return false;
}

POJ 3268 Silver Cow Party (Dijkstra)的更多相关文章

  1. POJ 3268 Silver Cow Party (Dijkstra + 优先队列)

    题意:由n个牧场,编号1到n.每个牧场有一头牛.现在在牧场x举办party,每头牛都去参加,然后再回到自己的牧场.牧场之间会有一些单向的路.每头牛都会让自己往返的路程最短.问所有牛当中最长的往返路程是 ...

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

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

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

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

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

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

  5. POJ 3268 Silver Cow Party 最短路

    原题链接: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:65536KB     64bit IO Format:%I64d & %I64u Su ...

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

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

  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 (Dijkstra)

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

随机推荐

  1. Mahout之Canopy Clustering深入理解

    转自:http://www.cnblogs.com/vivounicorn/archive/2011/09/23/2186483.html Mahout学习——Canopy Clustering 聚类 ...

  2. QT输入输出(一) 之 QDataStream 测试

    QT提供了两个高级别的流类---QDataStream和QTextStream,可以从任意的输入输出设备读取或写入数据. QDataStream用于读写二进制数据,它的优点是:在读写数据的时候已经严格 ...

  3. Json.Net学习笔记

    http://www.cnblogs.com/xiaojinhe2/archive/2011/10/28/2227789.html Newtonsoft.Json(Json.Net)学习笔记 http ...

  4. 5分钟内使用React、Webpack与ES6构建应用

    http://blog.leapoahead.com/2015/09/12/react-es6-webpack-in-5-minutes/

  5. extjs tablepanel 高度自适应有关问题

    extjs tablepanel 高度自适应问题 项目中为了给客户好点的功能切换体验,想到了用extjs的tabpanel 在页面中用了tabpanel后,高度新打开的tab页的iframe 的高度总 ...

  6. C++学习笔记之继承

    一.基类和派生类 很多时候,一个类的对象也“是”另一个类的对象,如矩形是四边形,在C++中,矩形类Rectangle可以由四边形类Quad继承而来,于是,四边形类Quad是基类,矩形类Rectangl ...

  7. [转]O(n)回文子串算法 Manacher算法

    这里,我介绍一下O(n)回文串处理的一种方法.Manacher算法.原文地址:http://zhuhongcheng.wordpress.com/2009/08/02/a-simple-linear- ...

  8. 安装luinxWEB

    Webmin的安装很简单,下面就详细说一下安装步骤. 1.用ssh客户端软件登陆服务器2.切换目录到root下,命令是:cd /root/3.下载Webmin的安装文件,命令是:wget http:/ ...

  9. IOS格式规范

    IOS格式规范 目录 概述 日期格式 NSDateFormatter格式说明 概述 日期格式 声明时间格式:NSDateFormatter *date_formatter = [[NSDateForm ...

  10. C# Socket基础(一)之启动异步服务监听

    本文主要是以代码为主..NET技术交流群 199281001 .欢迎加入. //通知一个或多个正在等待的线程已发生事件. ManualResetEvent manager = new ManualRe ...