Silver Cow Party.(POJ-3268)

本来想用Floyd算法,可惜超时,毕竟复杂度太高,而且并没有必要求出任意两点间的最短距离。

求两点间的最短路有两种方法,dijkstra和Bellman ,前者不能有负圈,后者可以有负圈,另外,Floyd也可以求带负圈的最短距离。

我们只需要求出x到其他个点的最短距离和个点到它的最短距离就行了。当然,我所写的还求了很多多余的量,是可以优化的。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int INF = 1000000;
int n,m,x,a,b,c,d[1005][1005];
struct edge {
    int to,cost;
    edge(int to = 0,int cost = 0) : to(to),cost(cost) {}
 };
typedef pair<int,int> P;
vector<edge> G[1005];
void dijkstra(int s) {
    priority_queue<P, vector<P> ,greater<P> > que;
    d[s][s] = 0;
    que.push(P(0,s));
    while(!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if(d[s][v]<p.first) continue;
        for(int i=0;i<G[v].size();i++) {
            edge e = G[v][i];
            if(d[s][e.to]>d[s][v]+e.cost) {
                d[s][e.to] = d[s][v] + e.cost;
                que.push(P(d[s][e.to],e.to));
            }
        }
    }
}
int main() {
    scanf("%d%d%d",&n,&m,&x);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) d[i][j] = INF;
    for(int i=0;i<m;i++) {
        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back(edge(b,c));
    }
    dijkstra(x);
    for(int i=1;i<=n;i++) dijkstra(i);
        int sum = -1;
    for(int i=1;i<=n;i++) {
        int v = d[i][x]+d[x][i];
        sum = max(sum,v);
    }
    printf("%d\n",sum);
    return 0;
}
时间: 2024-10-03 12:26:07

Silver Cow Party.(POJ-3268)的相关文章

Silver Cow Party POJ - 3268 (固定起点和固定终点的最短路)

思路:有向图.假设在X牧场参加party,从X回家的时候,以X为起点,使用一次Dijkstra算法即可.难点在于去X参加party的最短路如何求解. 这时候我们可以反向建图,即把原来有向图的方向全部反向,形成一幅新的有向图G',此时再对G'使用一次以X为起点的Dijkstra算法即    可求得原图G中其他各点以X为终点的最短路径. 1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #include

DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. POJ 3268 //#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace

POJ 3268 Silver Cow Party(SPFA)

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 re

图论 ---- 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 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party 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

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party 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

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

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