题目1447:最短路

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <sstream>
#include <climits>
using namespace std;
//1447 dijkstra 二维矩阵
int n, m;
int map[1010][1010];
bool vi[1010];
int dis[1010];
 
const int INF = 2100000000;
 
void init()
{
    for (int i = 1; i <= n; ++ i)
    {
        vi[i] = false;
        dis[i] = INF;
        for (int j = 1; j <= n; ++ j)
        {
            map[i][j] = INF;
        }
    }
    for (int i = 0; i < m; ++ i)
    {
        int from, to, d;
        scanf("%d%d%d", &from, &to, &d);
        map[to][from] = map[from][to] = d;
    }
}
 
void dijkstra()
{
    dis[1] = 0;
    for (int k = 0; k < n; ++ k)
    {
        int index = -1;
        int minn = INF;
        for (int i = 1; i <= n; ++ i)
        {
            if (vi[i]==false && dis[i]<minn)
            {
                index = i;
                minn = dis[i];
            }
        }
        vi[index] = true;
        for (int j = 1; j <= n; ++ j)
            dis[j]=min(dis[j],dis[index]+map[index][j]);
    }
}
 
int main()
{
   // freopen("input.txt","r",stdin);
    while (cin >> n >> m, n || m)
    {
        init();
        dijkstra();
        cout << dis[n] << endl;
    }
    return 0;
}
 
/**************************************************************
    Problem: 1447
    User: cust123
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:5508 kb
****************************************************************/

#include "iostream"
#include "stdio.h"
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <string.h>
#include <stack>
#include <ctype.h>
#include <functional>
#include <math.h>
using namespace std;
//1447
const int INF=0x7f7f7f7f7f;
int n,m,a,b,c;
struct E{
    int next;
    int c;
};
vector<E> e[101];
bool mark[101];
int dis[101];
 
int main(){
    //freopen("input.txt","r",stdin);
    while(cin>>n>>m){
        if(n==0&&m==0)  break;
        for(int i=1;i<=n;i++)    e[i].clear();
        while(m--){
            cin>>a>>b>>c;
            E t;
            t.c=c;
            t.next=b;
            e[a].push_back(t);
            t.next=a;
            e[b].push_back(t);
        }
        for(int i=1;i<=n;i++){
            dis[i]=INF;
            mark[i]=false;
        }
        dis[1]=0;
        mark[1]=true;
        int np=1;
        for(int i=1;i<n;i++){
            for(int j=0;j<e[np].size();j++){
                int t=e[np][j].next;
                int c=e[np][j].c;
                if(mark[t]==true)   continue;
                if(dis[t]==INF || dis[t]>dis[np]+c)
                    dis[t]=dis[np]+c;
            }
            int mi=INF;
            for(int j=1;j<=n;j++){
                if(mark[j]==true) continue;
                if(dis[j]==INF)   continue;
                if(dis[j]<mi){
                    mi=dis[j];
                    np=j;
                }
            }
            mark[np]=true;
        }
        cout<<dis[n]<<endl;
    }
    return 0;
}
/**************************************************************
    Problem: 1447
    User: cust123
    Language: C++
    Result: Accepted
    Time:30 ms
    Memory:1520 kb
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值