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. Objc基础学习记录5

    NSMutableString类继承的NSString类. NSMutableString是动态的字符串. 1.appendingString 方式: 向字符串尾部添加一个字符串. 2.appendi ...

  2. 查看大图 zoomImage

    添加引用 <link rel="stylesheet" media="screen" type="text/css" href=&qu ...

  3. Servlet容器的启动过程

    [http://book.51cto.com/art/201408/448854.htm]   Tomcat的启动逻辑是基于观察者模式设计的,所有的容器都会继承Lifecycle接口,它管理着容器的整 ...

  4. 从零新建一个winform项目

    网站:https://community.devexpress.com/blogs/eaf/archive/2012/10/30/xaf-application-from-scratch.aspx

  5. vs2013修改默认的开发环境

    可能会有朋友和我一样,当安装完VS完成之后,没有选择默认的开发模板,在后面添加新项目时,总是不能选择默认的开发语言,下面给出个简单步骤,记录一下以备用. 看图吧 1.工具>导入和导出设置 2.选 ...

  6. url中的scheme

    iPhone上URL Schemes的作用为应用程序提供了一个其他应用程序或者safari可以启动他的方法. --http://blog.sina.com.cn/s/blog_5673c12f0100 ...

  7. sql 将一个表中的数据插入到另一个表中

    列名不一定要相同,只要你在HH中列出要插入列的列表跟select   from   mm表中的选择的列的列表一一对应就可以了,当然两边的数据类型应该是兼容的. 比如:insert   into   h ...

  8. 用java写一个web服务器

    一.超文本传输协议 Web服务器和浏览器通过HTTP协议在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议——客户端发送一个请求,服务器返回该请求的应答.HTTP协议使用可靠的T ...

  9. ASP.NET中DesignMode属性

    参考:http://blog.sina.com.cn/s/blog_4c9da9b50100r4u7.html http://book.51cto.com/art/200902/108836.htm ...

  10. codechef Arranging Cup-cakes题解

    Arranging Cup-cakes Our Chef is catering for a big corporate office party and is busy preparing diff ...