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 namespace std;
#define M 2009
#define INF 0x3f3f3f3f
struct edge
{
    int to,w;
};
bool inq[M];
vector <edge> g[M];
int dis[M];
int n,m;
void spfa(int s)
{
    for(int i = 1;i <= n;i++)
    {
        dis[i] = INF;
        inq[i] = false;
    }
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    inq[s] = true;
    while(!q.empty())
    {
        s = q.front();
        inq[s] = false;
        q.pop();
        for(int i = 0;i < g[s].size();i++)
        {
            int v = g[s][i].to;
            int w = g[s][i].w;
            if(dis[v] > dis[s]+w)
            {
                dis[v] = dis[s]+w;
                if(!inq[v])
                {
                    inq[v] = true;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    while(scanf("%d %d",&m,&n)==2)  //m n写反了···· 贡献好几次WA
    {
        for(int i = 1;i <= n;i++)
            g[i].clear();
        for(int i = 0;i < m;i++)
        {
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            edge e;
            e.to = b;e.w = c;
            g[a].push_back(e);
            e.to = a;
            g[b].push_back(e);  //无向图 变化成双向的。
        }
        spfa(n);
        printf("%d\n",dis[1]);
    }
    return 0;
}
时间: 2024-08-01 02:26:13

poj 2387 最短路 spfa 实现的相关文章

It&amp;#39;s not a Bug, It&amp;#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1353   Accepted: 516 Description It is a curious fact that consumers buying a new software product generally do not expect the software to

POJ 1511 最短路spfa

题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf 还是超时(过去并没有用cin的坏习惯..近两个星期才开始疯狂的使用cin..因为懒..) 后来想了一下 spfa也可以求单源最短路 就试着写了一发scanf 然后wa...看了半天题目 发现是有很大可能爆int的 改了后1800+msAC 用cin仍然超时 所以cin害人不浅 scanf大法好23

POJ 3159 最短路 SPFA

#include<iostream> using namespace std; const int nMax = 30005; const int mMax = 150005; const int inf = 1000000000; struct node{ int v, w, next; }edge[mMax]; int n, edgeHead[nMax], dict[nMax]; int stack[nMax]; bool vis[nMax]; void spfa(){ for(int i

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 quickly as possible. Farmer John's field ha

POJ 2387 最短路Dijkstra算法

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33607   Accepted: 11383 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: 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 2448(K短路,A*+SPFA) Remmarguts&#39; Date

题意 给一个n个点m条边的图,然后给一个起点和一个终点,求起点到终点的第K短路. 思路 求第K短路.一个经典的问题. SPFA+A* 核心思想在A*搜索的估计函数的建立上. F(x) = g(x) + h(x) 估价函数 = s到x的距离 + x到t的距离 估价函数的含义就是经过x这个点的路径的距离. 我们在搜索的时候每次选择估价函数较小的值,进行拓展.这样我们搜索到t点的状态出来顺序就是,最短路-次短路-.第三短路- 就减少了我们搜索的状态数. 代码实现上,实现一个估价函数的结构体,然后是实现

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

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