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. opencv win7 配置

    按照官网的配置方法,结果出现了下面的错误信息F:\eclipse C++ workspace\test\Debug/../src/test.cpp:18: undefined reference to ...

  2. 妙用缓存调用链实现JS方法的重载

    来自于我的博客http://sweets.cf/,转载注明出处 1.什么是方法重载 方法重载是指在一个类中定义多个同名的方法,但要求每个方法具有不同的参数的类型或参数的个数. 简而言之就是:方法重载就 ...

  3. 使用JAVA对字符串进行DES加密解密(修正问题)

    http://blog.csdn.net/seraphwh/article/details/6801123

  4. 负载均衡SESSION同步总结

    1.redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题 http://www.cnblogs.com/painsOnline/p/5194851.h ...

  5. dojo grid 分页

    dojo很强大,也很方便,但是缺少文档,只能看源代码,也挺好的,就是费时间... 网上找了一段代码(找不到原出处了,不好意思),也看了dojo自带的demo,放一段可以执行的页面代码这里.把ip换成自 ...

  6. C++的优秀特性1:引用

    (转载请注明原创于潘多拉盒子) 一本典型的C语言教科书的厚度大约是200页左右,而一本典型的C++教科书的厚度至少要500页.比如K&R的<The C Programming Langu ...

  7. maven中解决javax.servlet.jsp.PageContext cannot be resolved to a type

    在eclipse环境下用maven出现:javax.servlet.jsp.PageContext cannot be resolved to a type. 这是由于没有引入jsp-api引发的问题 ...

  8. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  9. Codeforces Round #181 (Div. 2) A. Array 构造

    A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...

  10. 认清Linux中标准输入和标准输出的双重含义

    按照惯例,UNIX系统shell使用文件描述符0与进程的标准输入(一般是键盘)相关联,文件描述符1与标准输出(一般是显示器)相关联,文件描述符2与标准出错输出(一般是显示器)相关联. 在依从POSIX ...