BZOJ 1631 Cow Party

2*spfa.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define maxv 1050
#define maxe 200500
#define inf 2000000000
using namespace std;
queue <int> q;
int n,m,s,x,y,z,g[maxv],nume=0,dis[maxv][3];
struct edge
{
    int v,w,nxt;
}e[maxe];
bool vis[maxv];
void addedge(int u,int v,int w)
{
    e[++nume].v=v;
    e[nume].w=w;
    e[nume].nxt=g[u];
    g[u]=nume;
}
void spfa(int type)
{
    for (int i=1;i<=n;i++)
    {
        dis[i][type]=inf;
        vis[i]=false;
    }
    dis[s][type]=0;vis[s]=true;q.push(s);
    while (!q.empty())
    {
        int head=q.front();q.pop();
        for (int i=g[head];i;i=e[i].nxt)
        {
            int v=e[i].v;
             if ((i%2==type) && (dis[v][type]>dis[head][type]+e[i].w))
            {
                dis[v][type]=dis[head][type]+e[i].w;
                if (!vis[v]) {vis[v]=true;q.push(v);}
            }
        }
        vis[head]=false;
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&s);
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        addedge(x,y,z);addedge(y,x,z);
    }
    spfa(0);
    spfa(1);
    int ans=0;
    for (int i=1;i<=n;i++)
        ans=max(ans,dis[i][0]+dis[i][1]);
    printf("%d\n",ans);
    return 0;
}
时间: 2024-10-14 10:51:24

BZOJ 1631 Cow Party的相关文章

[BZOJ] 1631: [Usaco2007 Feb]Cow Party

1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 866  Solved: 624[Submit][Status][Discuss] Description 农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前去X牛棚参加派对还是返回住所,她们都采用了用时最

BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )

这道题和蔡大神出的今年STOI初中组的第二题几乎一模一样... 先跑一遍最短路 , 再把所有边反向 , 再跑一遍 , 所有点两次相加的最大值即为answer ----------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #in

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 

BZOJ 4761 Cow Navigation

一开始以为瞎jb写个IDA*就过了..然而并不是这样.(naive) 我的做法是dp[x1][y1][x2][y2][d1][d2],表示两头奶牛所在的位置和面朝的方向,然后直接spfa搞定. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define maxn 25 using namespace std; i

BZOJ 1648 Cow Picnic 奶牛野餐

作为傻逼的我,一开始就想到了DFS的正解.但是看了看数据范围 DFS的时间复杂度是(K+E)*NoW K是点 E是边 NOW是奶牛数量 掂量了下 觉得过不去,苦思冥想之后.看了下题解,卧槽这么简单吗? 发现自己看错了,把K+E 看成了K*E,惭愧惭愧~ 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4   5 #define up(a,b,c) for(int c = a;c<=b;

【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijskstra)

http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用spfa(可是好像dij比spfa还慢了在这里?) #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include &

【BZOJ】【3301】【USACO2011 Feb】Cow Line

康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 序列->序号:(康托展开) 对于每个数a[i],数比它小的数有多少个在它之前没出现,记为b[i],$ans=1+\sum b[i]* (n-i)!$ 序号->序列:(逆康托展开) 求第x个排列所对应的序列,先将x-1,然后对于a[i],$\left\floor \frac{x}{(n-i)!} \right\floor $即为在它之后出现的比它小的数的个数,所以从小到大数一下有几个没出现的数,就知道a[

[BZOJ] 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛

1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1312  Solved: 736[Submit][Status][Discuss] Description 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John在某个时刻看见贝茜在位置 (R1, C1),恰好T

bzoj:1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会

Description The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round Dance. Only cows can perform the