zoukankan      html  css  js  c++  java
  • poj1125(Stockbroker Grapevine)

    题目大意:

        题目会给你N个经纪人,样例给出的是第几个经纪人与其他经纪人之间的联系和散播需要的时间,问你从哪个经纪人开始传播是散发的谣言最快,以及算出所需要的时间,这里的时间是一条最短路径中所花费时间最少路径。如果存在某个经济人不在最短路中输出”disjoint“。(我没判断就过了,数据shui)

    解题思路:

        

        最短路径算法dijstra, 循环每一个经纪人,找出花费时间最少路径也就是题目要求的答案。

    代码:

    #include <algorithm>
    #include <iostream>
    #include <sstream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <string>
    #include <bitset>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <list>
    //#include <map>
    #include <set>
    using namespace std;
    /***************************************/
    #define ll long long
    #define int64 __int64
    /***************************************/
    const int INF = 0x7f7f7f7f;
    const double eps = 1e-8;
    const double PIE=acos(-1.0);
    const int d1x[]= {0,-1,0,1};
    const int d1y[]= {-1,0,1,0};
    const int d2x[]= {0,-1,0,1};
    const int d2y[]= {1,0,-1,0};
    const int fx[]= {-1,-1,-1,0,0,1,1,1};
    const int fy[]= {-1,0,1,-1,1,-1,0,1};
    /***************************************/
    void openfile()
    {
        freopen("data.in","rb",stdin);
        freopen("data.out","wb",stdout);
    }
    /**********************华丽丽的分割线,以上为模板部分*****************/
    int map[500][500];
    int lowcost[500];
    int vis[500];
    int n;
    int ce;
    int maax;
    void dijstra(int sta)
    {
        int i,j,k=0;
        int cnt=0;
        int min;
        for(i=1; i<=n; i++)
        {
            lowcost[i]=map[sta][i];
            vis[i]=0;
        }
        vis[sta]=1;
        // lowcost[sta]=0;
        for(i=1; i<n; i++)
        {
            min=INF;
            for(j=1; j<=n; j++)
            {
                if (!vis[j]&&lowcost[j]<min)
                {
                     min=lowcost[j];
                     k=j;
                }
            }
            if (k)
                cnt++;
            if (cnt==n-1)
                if (min<maax)
                {
                    maax=min;
                    ce=sta;
                }
            vis[k]=1;
            for(j=1;j<=n;j++)
                if(!vis[j]&&lowcost[j]>lowcost[k]+map[k][j])
                    lowcost[j]=lowcost[k]+map[k][j];
        }
    }
    int main()
    {
        while(scanf("%d",&n)&&n)
        {
            int i,j;
            for(i=0; i<500; i++)
                for(j=0; j<500; j++)
                    map[i][j]=INF;
            int a;
            int u,cost;
            for(i=1; i<=n; i++)
            {
                scanf("%d",&a);
                for(j=0; j<a; j++)
                {
                    scanf("%d%d",&u,&cost);
                    map[i][u]=cost;
                }
            }
            maax=INF;
            for(i=1; i<=n; i++)
                dijstra(i);
            printf("%d %d
    ",ce,maax);
        }
        return 0;
    }
    //没有判断disjoint 。可以自己加上。
    View Code
    屌丝终有逆袭日,*******。
  • 相关阅读:
    2019.6.20刷题统计
    36 线程 队列 守护线程 互斥锁 死锁 可重入锁 信号量
    35 守护进程 互斥锁 IPC 共享内存 的方式 生产者消费者模型
    34 进程 pid ppid 并发与并行,阻塞与非阻塞 join函数 process对象 孤儿进程与僵尸进程
    33 udp 域名 进程
    32 粘包 文件传输
    31 socket客户端. 服务器 异常 语法
    30 网络编程
    29 元类 异常
    26 封装 反射 常用内置函数
  • 原文地址:https://www.cnblogs.com/ZhaoPengkinghold/p/3833624.html
Copyright © 2011-2022 走看看