Dijkstra---用堆优化解决问题

《堆优化解决类似多源最短路问题》
1
1127. 香甜的黄油 2 3 农夫John发现了做出全威斯康辛州最甜的黄油的方法:糖。 4 5 把糖放在一片牧场上,他知道 N 只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。 6 7 当然,他将付出额外的费用在奶牛上。 8 9 农夫John很狡猾,就像以前的巴甫洛夫,他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。 10 11 他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。 12 13 农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛)。 14 15 给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。 16 17 数据保证至少存在一个牧场和所有牛所在的牧场连通。 18 19 输入格式 20 第一行: 三个数:奶牛数 N,牧场数 P,牧场间道路数 C。 21 22 第二行到第 N+1 行: 1 到 N 头奶牛所在的牧场号。 23 24 第 N+2 行到第 N+C+1 行:每行有三个数:相连的牧场A、B,两牧场间距 D,当然,连接是双向的。 25 26 输出格式 27 共一行,输出奶牛必须行走的最小的距离和。 28 29 数据范围 30 1≤N≤500, 31 2≤P≤800, 32 1≤C≤1450, 33 1≤D≤255 34 输入样例: 35 3 4 5 36 2 37 3 38 4 39 1 2 1 40 1 3 5 41 2 3 7 42 2 4 3 43 3 4 5 44 输出样例: 45 8

这道题如果直接用floyd来写的话,时间复杂度是O(p^3);

是5.12*1e8;很可能超时

但是如果对于每一个点都用堆优化的Dijkstra算法,时间复杂度是O(p*c*logn^2);

还可以

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f, N = 510;
vector<PII> g[2*N];//注意这里,我不知道为啥写成vector<vector<int,int>>g就会读入时,读不进
int n, p, c;
int wherecows[N];
int dist[2 * N];
bool st[2 * N];
void Dijkstra(int str)
{
    memset(dist, 0x3f, sizeof(dist));
    memset(st, false, sizeof(st));
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    dist[str] = 0;
    heap.push({0, str});
    while (heap.size())
    {
        int v = heap.top().second;
        heap.pop();
        if (st[v])
            continue;
        st[v] = true;

        for (int i = 0; i < g[v].size(); i++)
        {
            int node = g[v][i].first;
            int w = g[v][i].second;
            if (!st[node])
            {
                dist[node] = min(dist[node], dist[v] + w);
                heap.push({dist[node], node});
            }
        }
    }
}
int main()
{
    cin >> n >> p >> c;
    for (int i = 1; i <= n; i++)
    {
        cin >> wherecows[i];
    }
    int a, b, w;
    for (int i = 1; i <= c; i++)
    {
        cin >> a >> b >> w;
        g[a].push_back({b,w});
        g[b].push_back({a,w});
    }
    int mindist = INF;
    for (int i = 1; i <= p; i++)
    {
        Dijkstra(i);
        int nowdist = 0;
        for (int i = 1; i <= n; i++)
        {
            nowdist += dist[wherecows[i]];
        }
        mindist = min(mindist, nowdist);
    }
    printf("%d", mindist);
    return 0;
}
这段代码会超时,很神奇,我不知道怎么回事

 《堆优化的使用细节》

 1 1135. 新年好
 2  
 3 重庆城里有 n 个车站,m 条 双向 公路连接其中的某些车站。
 4 
 5 每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费的时间可能不同。
 6 
 7 在一条路径上花费的时间等于路径上所有公路需要的时间之和。
 8 
 9 佳佳的家在车站 1,他有五个亲戚,分别住在车站 a,b,c,d,e。
10 
11 过年了,他需要从自己的家出发,拜访每个亲戚(顺序任意),给他们送去节日的祝福。
12 
13 怎样走,才需要最少的时间?
14 
15 输入格式
16 第一行:包含两个整数 n,m,分别表示车站数目和公路数目。
17 
18 第二行:包含五个整数 a,b,c,d,e,分别表示五个亲戚所在车站编号。
19 
20 以下 m 行,每行三个整数 x,y,t,表示公路连接的两个车站编号和时间。
21 
22 输出格式
23 输出仅一行,包含一个整数 T,表示最少的总时间。
24 
25 数据范围
26 1≤n≤50000,
27 1≤m≤105,
28 1<a,b,c,d,e≤n,
29 1≤x,y≤n,
30 1≤t≤100
31 输入样例:
32 6 6
33 2 3 4 5 6
34 1 2 8
35 2 3 3
36 3 4 4
37 4 5 5
38 5 6 2
39 1 6 7
40 输出样例:
41 21
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 #include <queue>
  5 using namespace std;
  6 typedef pair<int, int> PII;
  7 const int N = 50010, INF = 0x3f3f3f3f;
  8 int n, m, target[6];
  9 int x, y, t;
 10 vector<PII> g[N];
 11 int dist[6][N];//dist[i][j]表示点target[i]到点j的最短距离是dist[i][j];
 12 bool st[N], vis[6];
 13 int ans = INF;
 14 void Dijkstra(int x, int node)
 15 {
 16     memset(st, false, sizeof(st));
 17     dist[x][node] = 0;
 18     priority_queue<PII, vector<PII>, greater<PII>> heap;
 19     heap.push({0, node});
 20     while (heap.size())
 21     {
 22         int v = heap.top().second;
 23         heap.pop();
 24         if (st[v])
 25             continue;
 26         st[v] = true;
 27 
 28         //注意堆优化千万不能够用这个来,
 29         //也就是说,不能够用g[N][N]来保存图,因为用这个数据结构保存图
 30         //意为着一定会用到下面这种方式来更新路径
 31         //但这种方式,铁定时间复杂度是O(nm),m是路径条数,n是点数
 32         //y总所说的O(mlogn)是我们手写堆,定死其中元素个数是n
 33         //但是用STL中的堆,无法把控堆中总个数,可能其中个数是m
 34         //这时其实是O(mlogm),但在稀疏图中一般m<n^2
 35         //可以看做O(mlogm)约等于O(mlogn)
 36 
 37         /*  for (int i=1;i<=n;i++)
 38          {
 39              if (!st[i] && dist[x][i]>dist[x][v]+g[v][i])
 40              {
 41                  dist[x][i]=dist[x][v]+g[v][i];
 42                  heap.push({})
 43              }
 44          } */
 45          //分析这个时间复杂度:
 46          //首先这个for循环,最多总共也就是执行了m次
 47          //heap.size()总的也就是m次
 48          //堆自身内部排序logm(logn)
 49          //所以总时间复杂度O(mlogm)(O(mlogn));
 50         for (int i = 0; i < g[v].size(); i++)
 51         {
 52             int lnode = g[v][i].second;
 53             int w = g[v][i].first;
 54             if (!st[lnode])
 55             {
 56                 dist[x][lnode] = min(dist[x][lnode], dist[x][v] + w);
 57                 heap.push({dist[x][lnode], lnode});
 58             }
 59         }
 60     }
 61 }
 62 void dfs(int pre, int u, int road)
 63 {
 64     if (road >= ans)
 65         return;
 66     if (u > 5)
 67         ans = road;
 68     for (int i = 1; i <= 5; i++)
 69     {
 70         if (!vis[i])
 71         {
 72             int thisnode = target[i];
 73             vis[i] = true;
 74             int way;
 75             if (pre == 0)
 76                 way = dist[i][1];
 77             else
 78                 way = dist[pre][target[i]];
 79             dfs(i, u + 1, road + way);
 80             vis[i]=false;
 81         }
 82     }
 83 }
 84 int main()
 85 {
 86     memset(dist, 0x3f, sizeof(dist));
 87     cin >> n >> m;
 88     for (int i = 1; i <= 5; i++)
 89         cin >> target[i];
 90     for (int i = 1; i <= m; i++)
 91     {
 92         scanf("%d%d%d", &x, &y, &t);
 93         g[x].push_back({t, y});
 94         g[y].push_back({t, x});
 95     }
 96     for (int i = 1; i <= 5; i++)
 97         Dijkstra(i, target[i]);
 98     memset(vis, false, sizeof(vis));
 99     dfs(0, 1, 0);//上一个点是...,现在要枚举的是第...个点,现在的总路程是...
100     cout<<ans;
101     return 0;
102 }

 

posted @ 2022-06-23 17:02  次林梦叶  阅读(65)  评论(0)    收藏  举报