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. 基于MySQL协议的数据库中间层项目Atlas - 360团队

    一.简介 Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了 ...

  2. Framewrok损坏导致卸载不了的解决办法

    1 使用微软自己提供的卸载软件 msicuu2.exe 卸载所有Framework安装版本和更新信息 2 卸载成功后,最好重启一下电脑,然后,从低版本到高版本,安装framewrok,当然,这个按照个 ...

  3. Linux 禁用笔记本触摸板

    1. 查看有什么设备 xinput list 输出: ⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTEST p ...

  4. android 照相或从相册获取图片并裁剪

    照相或从相册获取图片并裁剪 在android应用中很多时候都要获取图片(例如获取用户的头像)就需要从用户手机上获取图片.可以直接照,也可以从用户SD卡上获取图片,但获取到的图片未必能达到要求.所以要对 ...

  5. WebService《JavaEE6权威指南 基础篇第4版》

    [Web服务] 为运行在不同平台和框架之上的软件提供了互操作的标准方式.良好的互操作性和可扩展性.消息采用自包含文档的形式. ——解决异构系统之间交互.解决异构系统通信问题:  1.通过XML,JSO ...

  6. [转]C/C++中的memset

    http://blog.csdn.net/songuooo/article/details/7819790 1. 需要的头文件 C中为<memory.h> 或 <string.h&g ...

  7. 当spring 容器初始化完成后执行某个方法 防止onApplicationEvent方法被执行两次

    在做web项目开发中,尤其是企业级应用开发的时候,往往会在工程启动的时候做许多的前置检查. 比如检查是否使用了我们组禁止使用的Mysql的group_concat函数,如果使用了项目就不能启动,并指出 ...

  8. 从零开始学android开发-项目打包发布

    右键项目 选择[android tools]-[export signed application package] 点击[next] 如果没有keystore可以选择[create new keys ...

  9. cdoj 1141 酱神寻宝 状压dp

    酱神寻宝 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1141 Descri ...

  10. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...