BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19226   Accepted: 8775

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

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.

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map[1001][1001],w[1001][1001];
int dis[1001],Team[5000];
bool exist[1001];
int n,m,t;        // exist 为 true 则不可进队
void SPFA(int s)
{
    int head=0,tail=1,k=0;
    Team[head]=s,exist[s]=true;dis[s]=0;
    while(head<tail)
    {
        k=Team[head];
        exist[k]=false;
        for(int i=1;i<=n;i++)
        {
            if((map[k][i]>0)&&(dis[i]>dis[k]+map[k][i]))
            {
                 dis[i]=map[k][i]+dis[k];
                 if(exist[i]==false)
                 {
                     Team[tail++]=i;
                     exist[i]=true;
                }
            }
        }
        head++;
    }
}
int main()
{
    cin>>n>>m>>t;
    memset(map,0x3f,sizeof map );
    memset(w,0x3f,sizeof w );
    memset(dis,0x3f,sizeof dis );
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        map[x][y]=z;
        w[x][y]=z;
    }
    SPFA(t);// 从起点开始到其他每个点找一遍最短路
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(w[i][j]>w[i][k]+w[k][j])
                   w[i][j]=w[i][k]+w[k][j];
            }
        }
    }
    int max;
    for(int i=1;i<=n;i++)
    {
        if((i==1)||(dis[i]+w[i][t]>max))
           max=dis[i]+w[i][t];

    }
    printf("%d",max);
    return 0;
}

思路:一遍SPFA求出起点到其他所有点的最短路,一遍Floyed求出所有点到起点的最短路,两者相加,和最大者即为anwser...

时间: 2024-12-14 03:42:16

BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对的相关文章

【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑

倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到E,恰好经过N条边的最短路径(姑且称为路径吧,虽然好像已经不是了……) 总共只有大约200个点(很多点根本没走到,离散化一下即可)所以可以考虑Floyd算最短路. 引用下题解: 题目求i,j之间边数恰为N的最短路径(边可以重复走),我们知道线性代数中有:01邻接矩阵A的K次方C=A^K,C[i][j

BZOJ 1706 usaco 2007 Nov relays 奶牛接力跑/POJ 3613 Cow Relays 倍增Floyd

题目大意:求恰好走k步从S到T的最短路. 思路:设f[p][i][j]为从i到j恰好走2^p步的最短路,DP方程十分简单:f[p][i][j] = min(f[p][i][j],f[p - 1][i][k] + f[p - 1][k][j]); 对总步数T进行二进制拆分,在T有1的位置上,假如这个位置为p,那么就用f[p][][]来更新答案g[][],最后得到的g[][]就是答案矩阵. 注意要离散化一下.. CODE: #include <cstdio> #include <cstrin

BZOJ 1641 USACO 2007 Nov. Cow Hurdles 奶牛跨栏

[题解] 弗洛伊德.更新距离的时候把$f[i][j]=min(f[i][j],f[i][k]+f[k][j])$改为$f[i][j]=min(f[i][j],max(f[i][k],f[k][j]))$. #include<cstdio> #include<algorithm> #include<cstring> #define N (400) #define rg register using namespace std; int n,m,t,a[N][N]; inl

[BZOJ 1647][USACO 2007 Open] Fliptile 翻格子游戏

1647: [Usaco2007 Open]Fliptile 翻格子游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 702  Solved: 281[Submit][Status][Discuss] Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a

POJ 2184 Cow Exhibition 奶牛展(01背包,严重变形)

题意:有只奶牛要证明奶牛不笨,所以要带一些奶牛伙伴去证明自己.牛有智商和幽默感,两者可为负的(难在这),要求所有牛的智商和之 / 幽默感之和都不为负.求两者之和的最大值. 思路:每只牛可以带或不带上,是01背包问题.但是问题是没有明显的背包容量限制,却有了不为负的一些限制,相同的是要求最大和.必须找个背包容量出来. 1)背包容量:可以使用幽默感之和或智商之和作为背包容量.两者是提供的有明确范围的. 2)负号的问题:牛最多100只,而智商与幽默感最多为正负1千,那么 -1000*100<=x<=

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 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 

POJ 3268 Silver Cow Party (来回最短路 SPFA)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14384   Accepted: 6490 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 ≤

POJ 3268 Silver Cow Party (Dijkstra)

题目链接:POJ 3268 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

POJ 3268 Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20274   Accepted: 9278 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