kuangbin_ShortPathA (POJ 2387)

最短路模板题 但是其实很费时间 因为要看明白dij floyd 以及 dij优化 spfa优化 交了三次 大概是理解了

不过涉及到priority_queue的重载运算符问题 以后要在C++里面好好看看 现在不理解

Dijkstra ver:

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <queue>
 7 #include <map>
 8 #include <set>
 9 #include <vector>
10 #include <algorithm>
11 #define INF 0x3F3F3F3F
12 using namespace std;
13 typedef pair<int, int> pii;
14
15 int size, head[101000], point[401000], next[401000], val[401000];
16 int dis[101000], t, n;
17
18 void add(int from, int to, int value)
19 {
20     point[size] = to;
21     next[size] = head[from];
22     val[size] = value;
23     head[from] = size++;
24 }
25
26 struct cmp{
27     bool operator () (pii a, pii b){
28         return a.first > b.first;
29     }
30 };
31
32 void dijkstra(int s)
33 {
34     memset(dis, 0x3F, sizeof dis);
35     priority_queue<pii, vector<pii>, cmp> q;
36     q.push(make_pair(0, s));
37     dis[s] = 0;
38     while(!q.empty()){
39         pii u = q.top();
40         q.pop();
41         if(u.first > dis[u.second]) continue;
42         for(int i = head[u.second]; ~i; i = next[i]){
43             int j = point[i];
44             if(dis[j] > u.first + val[i]){
45                 dis[j] = u.first + val[i];
46                 q.push(make_pair(dis[j], j));
47             }
48         }
49     }
50 }
51
52 int main()
53 {
54     while(~scanf("%d%d", &t, &n)){
55         size = 0;
56         memset(head, -1, sizeof head);
57         for(int i = 1; i <= t; i++){
58             int from, to, value;
59             scanf("%d%d%d", &from, &to, &value);
60             add(from, to, value);
61             add(to, from, value);
62         }
63         dijkstra(1);
64         printf("%d\n", dis[n]);
65     }
66     return 0;
67 }

Spfa ver:

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;

int head[100010], point[400010], next[400010], val[400010], size;

void add(int from, int to, int value)
{
    point[size] = to;
    val[size] = value;
    next[size] = head[from];
    head[from] = size++;

    point[size] = from;
    val[size] = value;
    next[size] = head[to];
    head[to] = size++;
}

void spfa(int s, int t)
{
    int dis[100010];
    bool vis[100010];
    memset(dis, 0x3f, sizeof dis);
    memset(vis, false, sizeof vis);
    queue<int> q;
    dis[s] = 0;vis[s] = true;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u]; ~i; i = next[i]){
            int j = point[i];
            if(dis[j] > dis[u] + val[i]){
                dis[j] = dis[u] + val[i];
                if(!vis[j]){
                    q.push(j);
                    vis[j] = true;
                }
            }
        }
    }
    printf("%d\n", dis[t]);
}

int main()
{
    int t, n;
    memset(head, -1, sizeof head);
    scanf("%d%d", &t, &n);
    while(t--){
        int a, b, value;
         scanf("%d%d%d", &a, &b, &value);
         add(a, b, value);
    }
    spfa(1, n);
    return 0;
}
时间: 2024-10-06 18:02:49

kuangbin_ShortPathA (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

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

[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] 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 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