poj3268 Silver Cow Party(最短路)

非常感谢kuangbin专题啊,这道题一开始模拟邻接表做的,反向边不好处理,邻接矩阵的话舒服多了。

题意:给n头牛和m条有向边,每头牛1~n编号,求所有牛中到x编号去的最短路+回来的最短路的最大值。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn = 1e3 + 5;
const int maxm = 1e5 + 5;
const int inf = 0x3f3f3f3f;
struct edge{
    int s, to, w, next;
} ed[maxm];
int n, m, x;
int mp[maxn][maxn], dis1[maxn], dis2[maxn];
bool vis[maxn];
inline int max( int a, int b ){
    return a>b ? a:b;
}

inline int min( int a, int b ){
    return a<b ? a:b;
}

inline int dij(){
    //先求了去到x的最短路
    memset( vis, 0, sizeof(vis) );
    memset( dis1, inf, sizeof(dis1) );
    dis1[x] = 0;
    for( int i=1; i<n; i++ ){
        int minid, MIN=inf;
        for( int j=1; j<=n ;j++ ) if( !vis[j] && MIN>dis1[j] ) MIN = dis1[minid=j];
        vis[minid] = 1;
        for( int j=1; j<=n; j++ ) if( !vis[j] ) dis1[j] = min(dis1[j], dis1[minid]+mp[minid][j]);
    }
    //接下来求回去的最短路
    memset( vis, 0, sizeof(vis) );
    memset( dis2, inf, sizeof(dis2) );
    dis2[x] = 0;
    for( int i=1; i<n; i++ ){
        int minid, MIN = inf;
        for( int j=1; j<=n; j++ ) if( !vis[j] && MIN>dis2[j] ) MIN = dis2[minid=j];
        vis[minid] = 1;
        for( int j=1; j<=n; j++ ) if( !vis[j] ) dis2[j] = min( dis2[j], dis2[minid]+mp[j][minid] );
    }
    //遍历求出res
    int res = -inf;
    for( int i=1; i<=n; i++ ) res = max( dis1[i]+dis2[i], res );
    return res;
}

int main(){
    // freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(0);    //加速cin 和 cout读取速度,但是这样的话就不能使用scanf了
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> x;
    memset( mp, inf, sizeof(mp) );
    for( int i=1; i<=n; i++ ) mp[i][i] = 0;
    for( int i=0; i<m; i++ ){
        int u, v, w;
        cin >> u >> v >> w;
        mp[u][v] = w;
    }
    cout << dij() << endl;

    return 0;
}

原文地址:https://www.cnblogs.com/WAautomaton/p/10932409.html

时间: 2024-11-10 17:36:11

poj3268 Silver Cow Party(最短路)的相关文章

POJ3268 Silver Cow Party —— 最短路

题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24527   Accepted: 11164 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co

POJ-3268 Silver Cow Party( 最短路 )

题目链接:http://poj.org/problem?id=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 road

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

POJ3268 Silver Cow Party 【Dijkstra】

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

POJ3268 Silver Cow Party(dijkstra+矩阵转置)

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

poj3268——Silver Cow Party(最短路+godv之力)

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

【POJ3268】Silver Cow Party 最短路

题意:一堆奶牛去某个地方,去了又回,然后求去回和的最大值. 题解:两遍最短路,结束,邻接矩阵存边可以避免建反图. #include <cstdio> #include <cstring> #include <algorithm> #define N 1005 #define inf 0x3f3f3f3f using namespace std; int map[N][N],n,m,s; int dist1[N],dist2[N]; bool visit[N]; void

poj3268 Silver Cow Party (SPFA求最短路)

其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #

POJ3268 Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18713   Accepted: 8561 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