BZOJ 3386 Usaco Til the Cows come home

最短路水题,没啥好解释的。。

#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
 
 
const int maxv=2333;
const int INF=0x3f3f3f3f;
 
int n,m;
 
struct edge{
    int from, to, cost;
    edge() {}
    edge(int _from, int _to, int _cost) {
        from = _from;
        to = _to;
        cost = _cost;
    }
};
 
vector<int>G[maxv];
vector<edge> edges;
int rank[maxv], dis[maxv];
bool inque[maxv];
 
void add(int u, int v, int w) {
    edges.push_back(edge(u, v, w));
    int m = edges.size();
    G[u].push_back(m-1);
}
 
int spfa(int s, int n) {
    for (int i = 0; i <= n; i++) {
        dis[i] = INF;
        rank[i] = 0;
        inque[i] = false;
    }
    dis[s] = 0;
    rank[s] = 1;
    inque[s] = true;
    queue<int>que;
    que.push(s);
    while (!que.empty()) {
        int u = que.front();
        inque[u] = false;
        que.pop();
        for (int i = 0; i < (int)G[u].size(); i++) {
            edge e = edges[G[u][i]];
            if (dis[e.to] > dis[u] + e.cost) {
                dis[e.to] = dis[u] + e.cost;
                if (!inque[e.to]) {
                    que.push(e.to);
                    inque[e.to] = true;
                    rank[e.to]++;
                    if (rank[e.to] >= n) return false;
                }
            }
        }
    }
    return true;
}
 
int main(){
    memset(dis,0x3f3f3f3f,sizeof(dis));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    if(spfa(1,m)) printf("%d\n",dis[m]);
    return 0;
}
时间: 09-06

BZOJ 3386 Usaco Til the Cows come home的相关文章

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted: 12836 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

poj 2387 Til the Cows Come Home(dijkstra算法)

题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int map[2010][2010],Min,node[2010],vis[2010],t,q; 5 const int INF=9999999; 6 7 vo

POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Accepted: 15899 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for

POJ 2387 Til the Cows Come Home Dijkstra求最短路径

Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Til the Cows Come Home

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer John's field ha

POJ-2387 Til the Cows Come Home (最短路+Dijkstra)

Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer Joh

BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; ++i ) #defin

BZOJ 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛( dp )

树形dp..水 ------------------------------------------------------------------------ #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define clr( x , c )