poj2387(最短路)

题目连接:http://poj.org/problem?id=2387

题意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离。

分析:最短路裸题。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-9
#define N 1010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct node
{
    int v,w;
    node(){}
    node(int v,int w):v(v),w(w){}
    bool operator<(const node &a)const
    {
        return w>a.w;
    }
};
int dp[N],vis[N],n,m;
vector<node>g[N];
int dij()
{
    priority_queue<node>que;
    while(!que.empty())que.pop();
    FILL(dp,0x3f);FILL(vis,0);
    node cur,nxt;
    cur.v=n;cur.w=0;
    dp[n]=0;
    que.push(cur);
    while(!que.empty())
    {
        cur=que.top();que.pop();
        int x=cur.v;
        if(vis[x])continue;
        vis[x]=1;
        for(int i=0,sz=g[x].size();i<sz;i++)
        {
            nxt=g[x][i];
            int v=nxt.v,w=nxt.w;
            if(dp[x]+w<dp[v])
            {
                dp[v]=dp[x]+w;
                que.push(node(v,dp[v]));
            }
        }
    }
    return dp[1];
}
int main()
{
    int u,v,w;
    while(scanf("%d%d",&m,&n)>0)
    {
        for(int i=1;i<=n;i++)g[i].clear();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            g[u].push_back(node(v,w));
            g[v].push_back(node(u,w));
        }
        printf("%d\n",dij());
    }
}

时间: 2024-12-19 07:32:29

poj2387(最短路)的相关文章

poj2387 最短路

题意:给出一堆双向路,求从N点到1点的最短路径,最裸的最短路径,建完边之后直接跑dij或者spfa就行 dij: 1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 typedef pair<int,int> pii; 8 const in

最短路Floyd(hdu1874),dijstra(poj2387)

Floyd算法,多源最短路,O(n^3) 所以时间很受限制-- 主要注意细节,记住简单的三层for循环就好 1.初始化输入: 多样例,所以数组清空 注意重边情况,注意自己到自己是0 2.三层for 循环遍历每个点k, 循环计算map[i][j],看i->j最小还是i->k->j最小. hdu1874 #include <cstdio> #include <cstring> #define MAX 202 #define min(x,y) x<y?x:y #d

poj2387 初涉最短路

前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码~ #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int tance[1005][1005]; bool vis[1005]; int dis[1005]; int

POJ2387 水水最短路

迪杰斯特拉哦,很挫哦 A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2387 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possib

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

题目链接: http://poj.org/problem?id=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 ba

POj2387——Til the Cows Come Home——————【最短路】

A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice 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 F

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

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 possible. Farmer Joh

poj2387 spfa求最短路

1 //Accepted 4688 KB 63 ms 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <queue> 6 #include <cmath> 7 #include <algorithm> 8 using namespace std; 9 /** 10 * This is a documentation comment bl

超水的一道最短路poj2387

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 68667   Accepted: 23016 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