【最短路】 poj 2387

#include <iostream>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
using namespace std;
int map[1010][1010];
int dis[1010];
int n,pos,sum;
void init()
{
 for(int i=0; i<1001; i++)
 {
  for(int k=0; k<1001; k++)
   map[i][k] = INT_MAX;
  map[i][i] = 0;
 }
 for(int i=0; i<1001; i++)
  dis[i] = INT_MAX;
 sum = 0;
}
void Dijkstra()
{
 int used[1010];
 memset(used,0,sizeof(used));
 int now = pos;
 dis[now] = 0; used[now] = 1;
 for(int i=0; i<n; i++)
 {
  for(int k=1; k<=n; k++)
   if( map[now][k] != INT_MAX && dis[now] + map[now][k] < dis[k] )
    dis[k] = dis[now] + map[now][k];
  int min = INT_MAX;
  for(int k=1; k<=n; k++)
   if( dis[k] < min && !used[k] )
    min = dis[now = k];
  used[now] = 1;
 }
}
int main()
{
 int from,to,len,t;
 init();
 cin >> t >> n;
 pos = n;
 for(int i=1; i<=t; i++)
 {
  cin >> from >> to >> len;
  if( len < map[from][to] )
   map[from][to] = map[to][from] = len;
 }
 Dijkstra();
 cout << dis[1] << endl;
return 0;
}

#include<iostream>
using namespace std;
#define MAX 1005
const int oo=10000000;
int dist[MAX][MAX];
int close[MAX];
bool used[MAX];
int main()
{
    int N,T,i,j;
    int s,e,len;
    scanf("%d%d",&T,&N);
    memset(dist,0x7f,sizeof(dist));
    memset(used,false,sizeof(used));
    memset(close,0x7f,sizeof(close));
    for(i=1;i<=T;i++)
    {
        scanf("%d%d%d",&s,&e,&len);
        if(dist[s][e]>len)
            dist[s][e]=dist[e][s]=len;
    }
    for(i=2;i<=N;i++)
    {
        close[i]=dist[1][i];
    }
    used[1]=true;
    for(i=2;i<=N;i++)
    {
        int min=0;int mlen=oo;
        for(j=2;j<=N;j++)
        {
            if(!used[j]&&close[j]<mlen)
            {
                min=j,mlen=close[j];
            }
        }
        used[min]=true;
        for(j=2;j<=N;j++)
        {
            if(!used[j]&&dist[min][j]<oo)
            {
                int temp=dist[min][j]+close[min];
                if(temp<close[j])
                    close[j]=temp;
            }
        }
    }
    printf("%d\n",close[N]);
    return 0;
}
时间: 2024-10-10 16:52:57

【最短路】 poj 2387的相关文章

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa

[2016-04-02][POJ][2387][Til the Cows Come Home]

时间:2016-04-02 10:34:36 星期六 题目编号:[2016-04-02][POJ][2387][Til the Cows Come Home] 题目大意:给定n个节点和t条路,求n到1的最短路长度 分析:跑一次最短路即可 遇到的问题: 据说是多重边,如果是用邻接矩阵的就要更新最小值, 此题是先输入t,再输入n,输入的时候读错,无限WA- #include <queue> #include <cstring> #include <cstdio> using

kuangbin专题专题四 Til the Cows Come Home POJ - 2387

题目链接:https://vjudge.net/problem/POJ-2387 题意:从编号为n的城市到编号为1的城市的最短路. 思路:dijkstra模板题,直接套板子,代码中我会带点注释给初学者看. 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #include <string> 6 using namespac

POJ 2387 Til the Cows Come Home (Dijkstra)

题目链接:POJ 2387 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possib

POJ 2387

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27591   Accepted: 9303 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

poj 2387 Til the Cows Come Home(dijkstra算法)

题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int map[2010][2010],Min,node[2010],vis[2010],t,q; 5 const int INF=9999999; 6 7 vo

[POJ - 2387] L - Til the Cows Come Home(图论)

L - Til the Cows Come Home POJ - 2387 Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quick

poj 2387 Til the Cows Come Home(最短路)

题目链接:http://poj.org/problem?id=2387 题目大意:求点1到点n的最短距离 1.Dijkstras算法 #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 1010 #define INF 0x3f3f3f int map[maxn][maxn]; int vis[maxn]; //标记最短路的点 int dis

poj 2387 最短路 spfa 实现

http://poj.org/problem?id=2387 题目大意就是求最短路,从n到1的最短路.就用来熟悉一下spfa的写法. 一开始贡献了好几次wa,结果发现是因为n,m写反了.... 没有什么拐弯的地方,来熟悉spfa直接附上代码: #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> using names