Silver Cow Party(最短路)

原题链接
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19130 Accepted: 8734
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: N, M, and X
Lines 2…M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, 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
Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
Source

USACO 2007 February Silver

题目大意就是说有n个农场,每个农场有一头奶牛要去给定的农场x参加party,所有的奶牛去的时候是选择的最短的路径,回来的时候也是选择的最短的路径,问哪个奶牛需要的来回的时间是最长的
用floyd_warshall算法会超时,因为毕竟是100010001000的复杂度,所以就需要进行优化
下面是floyd_warshall的算法

#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
int n,m,x;
int d[maxn][maxn];
void floyd_warshall(){
        for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                        for(int k=1;k<=n;k++)
                                d[i][j]=min(d[i][j] , d[i][k] + d[k][j]);
        return;
}
int main(){
        scanf("%d%d%d",&n,&m,&x);
        for(int i=0;i<maxn;i++)
                for(int j=0;j<maxn;j++)
                        if(i==j) d[i][j]=0;
                        else d[i][j]=INF;
        while(m--){
                int x,y,val;
                scanf("%d%d%d",&x,&y,&val);
                d[x][y]=val;
        }
        floyd_warshall();
        int res=-1;
        for(int i=1;i<=n;i++)
                res=max(res,d[i][x]+d[x][i]);
        cout << res << endl;
        return 0;
}

下面是部分测试数据
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

4 7 2
1 2 7
2 1 3
1 3 4
3 1 2
1 4 3
4 3 5
2 3 2

4 8 2
1 2 10
2 1 1
1 3 4
3 1 6
4 1 2
3 4 4
2 3 3
3 2 1
output
10
20
14

用Bellman_ford算法来实现仅仅是O(|E||v||v|)的复杂度,当然其实用堆实现的dijkstra算法会更快

#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
int n,m,x;
int cost[maxn][maxn];
struct edge{int from,to,cost;}E[1000010];
//从b点到e点的最短路径
int Bellman_ford(int b,int e){
        int d[maxn];
        for(int i=1;i<=n;i++) d[i]=INF;
        d[b]=0;
        while(true){
                bool flag=false;
                for(int i=0;i<m;i++){
                        edge t=E[i];
                        if(d[t.from]!=INF && d[t.to] > d[t.from] + t.cost){
                                d[t.to]=d[t.from] + t.cost;
                                flag=true;
                        }
                }
                if(!flag) break;
        }
        return d[e];
}
int main(){
        scanf("%d%d%d",&n,&m,&x);
        for(int i=0;i<maxn;i++){
                for(int j=0;j<maxn;j++){
                        if(i==j) cost[i][j]=0;
                        else cost[i][j]=INF;
                }
        }
        for(int i=0;i<m;i++)
                scanf("%d%d%d",&E[i].from,&E[i].to,&E[i].cost);
        int res=-1;
        for(int i=1;i<=n;i++)
                res=max(res,Bellman_ford(x,i) + Bellman_ford(i,x));
        cout << res << endl;
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值