poj3268 Silver Cow Party (shortest)

Thank you very much kuangbin topic ah, This question is beginning to do the simulation adjacent table, the reverse side of hard to deal with, adjacency matrix, then more comfortable.

Question is intended: to cattle and n and m have the edges, each cow numbers 1 ~ n, seeking all the cattle go to x + shortest path back to the maximum value of the shortest.

#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++ ){
        intMinID MIN = inf;
        for ( int j = 1 ; j <= n; j ++) if (! view [j] && MIN> dis1 [j]) MIN = dis1 [MinID = j]; 
        view [MinID] = 1 ;
        for ( int j = 1 ; j <= n; j ++) if (! view [j]) dis1 [j] = min (dis1 [j], dis1 [MinID] + mp [MinID] [j]); 
    } 
    // 接下来求回去的最短路 
    MEMS (view, 0 , sizeof (view)); 
    MEMS (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 () {
     // The freopen ( "in.txt", "R & lt", stdin) ; 
    iOS :: sync_with_stdio ( 0 );     // acceleration cin and cout reading speed, but then can not use the scanf 
    cin.tie ( 0 ); 
    cout.tie ( 0 ); 
    cin >> >> m >> n- X; 
    Memset (MP, INF, the 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;
}

 

Guess you like

Origin www.cnblogs.com/WAautomaton/p/10932409.html