poj1125 Stockbroker Grapevine 最短路 dijkstral + 优先队列

//    poj1125 Stockbroker Grapevine 最短路 dijkstral + 优先队列
//
//    一个模板吧,留着纪念

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

typedef pair<int,int> P;

const int MAX_N = 108;
const int INF = 0x3f3f3f3f;
struct node {
    int to;
    int w;
    int next;

    node (){

    }

    node (int to,int w,int next):to(to),w(w),next(next){

    }
};

node edges[MAX_N * MAX_N];

int head[MAX_N];

int num;
int n,m;
bool vis[MAX_N];

void add_edge(int u,int v,int w){
    edges[num].to = v;
    edges[num].w = w;
    edges[num].next = head[u];
    head[u] = num++;
}

void input(){
    memset(head,-1,sizeof(head));
    num = 0;
    int v,w;
    for (int i=1;i<=n;i++){
        scanf("%d",&m);
        for (int j=1;j<=m;j++){
            scanf("%d%d",&v,&w);
            add_edge(i,v,w);
        }
    }
}

int dijkstra(int s){
    int d[MAX_N];
    memset(d,INF,sizeof(d));
    memset(vis,0,sizeof(vis));
    d[s] = 0;
    vis[s] = 1;
    priority_queue<P,vector<P>,greater<P> > que;
    que.push(P(0,s));

    while(!que.empty()){
        P x = que.top();
        que.pop();
        int u = x.second;
        if (x.first > d[u])    continue;
        for (int j = head[u] ; j!=-1;j=edges[j].next){
            int v = edges[j].to;
            int w = edges[j].w;
            if (d[v] > d[u] + w){
                d[v] = d[u] + w;
                que.push(P(d[v],v));
            }
        }
    }
    int mx = 0;
    for (int i=1;i<=n;i++){
        if (d[i]==INF){
            return -1;
        }
        else if (d[i] > mx){
            mx = d[i];
        }
    }
    return mx;

}

void solve(){
    int k = -1;
    int mx = INF;
    for (int i=1;i<=n;i++){
        int x = dijkstra(i);
        if (x!=-1){
            if (mx > x){
                k = i;
                mx = x;
            }
        }
    }
    printf("%d %d\n",k,mx);
}

int main(){
    //freopen("1.txt","r",stdin);
    while(scanf("%d",&n)!=EOF && n){
        input();
        solve();
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 02:22:59

poj1125 Stockbroker Grapevine 最短路 dijkstral + 优先队列的相关文章

Stockbroker Grapevine(最短路_floyd())

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27478   Accepted: 15223 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th

POJ1125 Stockbroker Grapevine【Floyd】

Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27977 Accepted: 15527 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the st

POJ1125——Stockbroker Grapevine

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27547   Accepted: 15264 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th

POJ1125 Stockbroker Grapevine 【Floyd】

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27431   Accepted: 15201 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th

POJ1125 Stockbroker Grapevine 多源最短路 Floyd

Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For maximum effect, you have to

POJ1125 Stockbroker Grapevine 多源最短路

题目大意 给定一个图,问从某一个顶点出发,到其他顶点的最短路的最大距离最短的情况下,是从哪个顶点出发?需要多久? (如果有人一直没有联络,输出disjoint) 解题思路 Floyd不解释 代码 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; const int INF = 1000000000; const

【POJ1125】Stockbroker Grapevine 最短路

题意:Floyd!!!直接说输入格式你们一定会做. 就是说多组数据,然后每组先一个n,然后n行,一个数是有几条出边(单向边),然后每条出边俩数分别为点和边权. 好了,现在求的是点x,使从x出发最远的点 最近,不懂直接看代码,风格良好!!! #include <cstdio> #include <cstring> #include <algorithm> #define N 105 #define inf 0x3f3f3f3f using namespace std; i

POJ1125 Stockbroker Grapevine

Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For maximum effect, you have to

【POJ 1125】Stockbroker Grapevine

[POJ 1125]Stockbroker Grapevine 最短路 不过这题数据很水..主要想大牛们试试南阳OJ同题 链接如下: http://acm.nyist.net/JudgeOnline/talking.php?pid=426&page=2 数据增大很多 用到很多东西才能过 (弱没过,,, 这题就是求最短路寻找所有通路中最大权的最小值外加考验英语水平-- Floyd 208K 0MS 1162B #include using namespace std; int dis[111][1