USACO07FEB银牛派对

【题意】n个点m条有向边,求单源最短路最长往返距离

【解题】建两个图

跑两遍spfa

不过有很多细节需要注意

#include<bits/stdc++.h>
const int N = 1005;
struct node{
    int v,w;
};
std::vector<node>g[3][N];

std::queue<int>q;
int n,k,m,x,y,z,hs,vis[N],f[3][N],ans = -0x3f3f;

void spfa(int k){

    memset(vis,0,sizeof vis);
    vis[hs] = 1;
    f[k][hs] = 0;
    q.push(hs);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        //std::cout << "DEBUG!"<<g[k][u].size();
        for(int i = 0;i < g[k][u].size();++i){
            int v = g[k][u][i].v, w = g[k][u][i].w;

            if(f[k][v] > f[k][u] + w){
                f[k][v] = f[k][u] + w;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}

int main(){
  scanf("%d%d%d",&n,&m,&hs);
  memset(f,0x3f3f3f,sizeof(f));
  for(register int i = 1; i <= m; ++i){
      scanf("%d%d%d",&x,&y,&z);
      g[1][x].push_back((node){y,z});
      g[2][y].push_back((node){x,z});
  }
  spfa(1);
  spfa(2);

  for(register int i = 1; i <= n; ++i){
       if(i == hs)continue;
       ans = std::max(ans,f[1][i] + f[2][i]);
  }
  printf("%d",ans);
  return 0;
}

原文地址:https://www.cnblogs.com/phemiku/p/11839388.html

时间: 2024-11-05 15:59:24

USACO07FEB银牛派对的相关文章

洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 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 co

luogu P1821 [USACO07FEB]银牛派对Silver Cow Party

题目描述 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

[USACO07FEB]银牛派对Silver Cow Party

题目描述 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度. 输入输出格式 输入格式: 第一行三个整数N,M, X: 第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti. 输出格式:

「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party

更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1000)\) conveniently numbered \(1 \cdots N\) is going to attend the big cow party to be held at farm #X \((1 \le X \le N)\). A total of \(M (1 \le M \l

浴谷夏令营题单

这是群里一位神犇整理的,我只负责将它们做完. 一.暴力.搜索Luogu 1588 丢失的牛Luogu 1463 [SDOI2005]反素数antBzoj 1085 [SCOI2005]骑士精神Luogu 1019 单词接龙Luogu 1078 文化之旅Luogu 1312 Mayan游戏Luogu 3823 蚯蚓排队Codeforces 444B Codeforces 555DLuogu 1979 华容道 二.初等数论Poj 3292 H合成数Luogu 1890 gcd区间Luogu 1029

【2019寒假集训DAY2】图论

Dijkstra 条件:无负权:时间:O(n*n) 详解+模板: https://www.cnblogs.com/jason2003/p/7222182.html (模板)P1821 [USACO07FEB]银牛派对Silver Cow Party:https://www.luogu.org/recordnew/show/12339877 void dj(){ for(int i=1;i<=m;i++) if(i!=k)dis[i]=M; for(int i=1;i<=m;i++){ int

推荐题目

二分 P2678 跳石头 P2440 木材加工 P1873 砍树 P1577 切绳子 P1824 进击的奶牛 P1316 丢瓶盖 UVA1555 Garland P1257 平面上的最接近点对 P1429 平面最近点对(加强版)(未完成) P1182 数列分段"Section II" P1281 书的复制(二分做法和上题一样,只是输出值变成了输出划分方式) 递推 P2386 放苹果 P1025 数的划分 DP&&MS P1091 合唱队形 P1140 相似基因 P144

帝都四日游

每次回家都路过北京,可是从来没去逛过,遗憾啊,今年决心去逛一逛 Day1,2/24,沈阳北-北京,国家博物馆,天安门,午门,西单,半日游 动车去的北京¥205,应该有更便宜的普通车的.4小时到北京,温度跟沈阳差不多,出站的时候就有卖公交一卡通的,应该直接买一张,当时急着出站,也怕是假的,就没敢买,出站过天桥去坐公交,我擦,这么多人,我的包很重(再也不背这么重的东西了,直接寄过来多好),我出汗了.脱下来棉袄,还是去坐地铁,我擦,这地铁没有安全门,还是环形的,我往哪边去,研究了一下终于上车了,长椿街

Silver Cow Party--poj3268(SPFA)

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