HDU Today HDU2112

题意:大概意思就是求一个图的单源最短路,这样,嗯。

思路:和直接的单源最短路问题不一样的就是输入的不是地点的编号而是字符串,只需要对每个字符串进行编号就好了。

因为地点最多150,所以直接用邻接矩阵,方便。
本来用string+map实现过一个,结果3790ms。。。后来看了改成char数组快了不少,一千多毫秒。
代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 200;
const int INF = 0x3f3f3f3f;

int N;
int G[MAXN][MAXN];
int start, endd;
char stn[MAXN][35];
int tot;
int d[MAXN];
bool vis[MAXN];

void Add(char s[])
{
    for(int i=0; i<tot; i++){
        if(strcmp(s, stn[i]) == 0){
            return ;
        }
    }
    strcpy(stn[tot++], s);
}

int GetNum(char s[])
{
    for(int i=0; i<tot; i++){
        if(strcmp(s, stn[i]) == 0){
            return i;
        }
    }
}

int Dijkstra(int s)
{
    for(int i=0; i<tot; i++) {d[i] = INF; vis[i] = false;}
    d[s] = 0;

    for(int i=0; i<tot; i++){
        int x, m = INF;
        for(int j=0; j<tot; j++){
            if(!vis[j] && d[j]<m) m = d[x=j];
        }

        vis[x] = true;

        for(int j=0; j<tot; j++){
            if(!vis[j] && G[x][j]<INF && d[x]<INF && d[j]>d[x]+G[x][j]){
                d[j] = d[x] + G[x][j];
            }
        }
    }
}

void Init()
{
    for(int i=0; i<MAXN; i++){
        for(int j=0; j<MAXN; j++){
            G[i][j] = INF;
        }
    }
    tot = 0;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while(scanf("%d", &N)==1 && N!=-1){
        Init();
        char s[35], e[35];
        scanf("%s%s", s, e);
        Add(s);
        Add(e);
        while(N--){
            char a[35], b[35];
            int c;
            scanf("%s%s%d", a, b, &c);
            Add(a);
            Add(b);
            int x = GetNum(a);
            int y = GetNum(b);
            G[x][y] = c;
            G[y][x] = c;

        }

        Dijkstra(GetNum(s));

        printf("%d\n", d[GetNum(e)]<INF? d[GetNum(e)] : -1);
    }

    return 0;
}

用优先权队列实现的Dijkstra,只想说用优先权队列实现自己还是不怎么熟悉。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 200;
const int INF = 0x3f3f3f3f;

struct Node{
    int v, w;
    Node(){}
    Node(int a, int b): v(a), w(b){}
    bool operator < (const Node& n)const{
        return w > n.w;
    }
};

int N;
int G[MAXN][MAXN];
int start, endd;
char stn[MAXN][35];
int tot;
int d[MAXN];
bool vis[MAXN];

void Add(char s[])
{
    for(int i=0; i<tot; i++){
        if(strcmp(s, stn[i]) == 0){
            return ;
        }
    }
    strcpy(stn[tot++], s);
}

int GetNum(char s[])
{
    for(int i=0; i<tot; i++){
        if(strcmp(s, stn[i]) == 0){
            return i;
        }
    }
}

int Dijkstra(int s)
{
    for(int i=0; i<tot; i++) {d[i] = INF; vis[i] = false;}
    d[s] = 0;

    priority_queue<Node> que;
    que.push(Node(s, 0));

    while(!que.empty()){
        Node tmp = que.top(); que.pop();

        vis[tmp.v] = true;

        for(int i=0; i<tot; i++){
            if(!vis[i] && G[tmp.v][i]<INF && d[tmp.v]<INF && d[i]>d[tmp.v]+G[tmp.v][i]){
                d[i] = d[tmp.v] + G[tmp.v][i];
                que.push(Node(i, d[i]));
            }
        }
    }
}

void Init()
{
    for(int i=0; i<MAXN; i++){
        for(int j=0; j<MAXN; j++){
            G[i][j] = INF;
        }
    }
    tot = 0;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while(scanf("%d", &N)==1 && N!=-1){
        Init();
        char s[35], e[35];
        scanf("%s%s", s, e);
        Add(s);
        Add(e);
        while(N--){
            char a[35], b[35];
            int c;
            scanf("%s%s%d", a, b, &c);
            Add(a);
            Add(b);
            int x = GetNum(a);
            int y = GetNum(b);
            G[x][y] = c;
            G[y][x] = c;

        }

        Dijkstra(GetNum(s));

        printf("%d\n", d[GetNum(e)]<INF? d[GetNum(e)] : -1);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值