poj 1502 单源最短路径

一、题目大意

无向图,给出邻接矩阵的下半矩阵,要求源点1,到其他点最短时间(散播整个网络的最短时间)。

二、AC code

明显的单源最短路径

但是还是用了Floyd算法撞撞运气,毕竟是无向图,当然可以对Floyd优化,最后也可以A。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
//#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

#define MAXN 102
int map[MAXN][MAXN];

int main()
{
    //freopen("input.txt","r",stdin);

    int n;
    cin >> n;
    for (int i = 2; i <= n; ++i) {
        for (int j = 1; j < i; ++j) {

            string s;
            cin>>s;

            int tmp = INF;
            if(s != "x"){
                tmp = stringToNum<int > (s);
            }

            map[j][i] = map[i][j] = tmp;
        }
    }

    //floyd
    for (int k = 1; k <= n; ++k) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j < i; ++j) {

                map[j][i] = map[i][j] = min(map[i][j],map[i][k]+map[k][j]);
            }
        }
    }

    int time = -1;
    for (int i = 2; i <= n ; ++i) {
        time = time > map[1][i]? time : map[1][i];
    }
    cout<<time<<endl;

    return 0;
}

dijkstra:

重定向忘记删掉,WA一次,甚是可惜

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
//#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

#define MAXN 102
int map[MAXN][MAXN];
bool visited[MAXN];

void update(int minPos,int n) {

    for (int i = 1; i <= n; ++i) {

        if( !visited[i] && map[1][i] > map[1][minPos] + map[minPos][i] )
            map[i][1] = map[1][i] = map[1][minPos] + map[minPos][i];
    }
}

void dijkstra(int n) {

    visited[1] = 1;
    while(1) {

        int tmpMin = INF;
        int minPos = -1;
        for (int i = 2; i <= n; ++i) {

            if( !visited[i] && tmpMin > map[1][i] ) {
                //have not been visited && smaller

                tmpMin = map[1][i];
                minPos = i;
            }
        }

        if( -1 == minPos )
            break;

        visited[minPos] = 1;
        update(minPos,n); //update the map, if it's shorter to get through minPos
    }
}

int main()
{
    //freopen("input.txt","r",stdin);

    int n;
    cin >> n;
    for (int i = 2; i <= n; ++i) {
        for (int j = 1; j < i; ++j) {

            string s;
            cin>>s;

            int tmp = INF;
            if(s != "x"){
                tmp = stringToNum<int > (s);
            }

            map[j][i] = map[i][j] = tmp;
        }
    }

    dijkstra(n);

    int time = -1;
    for (int i = 2; i <= n ; ++i) {
        time = time > map[1][i]? time : map[1][i];
    }
    cout<<time<<endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值