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;
}
相关文章
|
7月前
Poj 3255(dijkstra求次短路)
Poj 3255(dijkstra求次短路)
43 0
POJ-2253,Frogger(最短路问题)
POJ-2253,Frogger(最短路问题)
|
人工智能 并行计算 网络架构
|
Java 机器学习/深度学习
UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 65817    Accepted Submission(s): 28794 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。
1150 0
|
人工智能
POJ 2370 Democracy in danger(简单贪心)
Democracy in danger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3388   Accepted: 2508 Description In one of the...
956 0
poj supermaket (贪心)
http://poj.org/problem?id=1456 #include #include #include using namespace std; struct nod { int a; int d; }; bool cmp(nod x,nod y) { return x.
697 0