bzoj1631 [Usaco2007 Feb]Cow Party(Dijkstra)

心情烦躁只好刷水题。。。正反两遍最短路即可。。

#include 
using namespace std;
#define N 1010
#define M 100010
#define ll long long
#define inf 0x3f3f3f3f
#define pa pair
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,s,h[N],h1[N],num=0,d[N],d1[N],ans=0;
bool f[N];
struct edge{
    int to,from,next,next1,val;
}data[M];
void Dijkstra(){
    priority_queuevector,greater >q;
    memset(d,0x3f,sizeof(d));
    q.push(make_pair(0,s));d[s]=0;
    while(!q.empty()){
        int x=q.top().second;q.pop();
        if(f[x]) continue;f[x]=1;
        for(int i=h[x];i;i=data[i].next){
            int y=data[i].to;
            if(d[x]+data[i].valvoid Dijkstra1(){
    priority_queuevector,greater >q;
    memset(d1,0x3f,sizeof(d1));memset(f,0,sizeof(f));
    q.push(make_pair(0,s));d1[s]=0;
    while(!q.empty()){
        int x=q.top().second;q.pop();
        if(f[x]) continue;f[x]=1;
        for(int i=h1[x];i;i=data[i].next1){
            int y=data[i].from;
            if(d1[x]+data[i].valint main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();s=read();
    while(m--){
        int x=read(),y=read(),val=read();
        data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
        data[num].from=x;data[num].next1=h1[y];h1[y]=num;
    }
    Dijkstra();Dijkstra1();
    for(int i=1;i<=n;++i) ans=max(ans,d[i]+d1[i]);
    printf("%d\n",ans);
    return 0;
}

你可能感兴趣的:(bzoj,最短路)