P4962 朋也与光玉【状态压缩+SPFA】

题目链接


  最初的时候,我用了N个点,以每个点为起点都去跑一遍SPFA,但是这样的做法只能获得56分,然后就TLE了。

  然后,就是想办法更进做法了,于是,可以琢磨一下题目中的每个点都是只能被经过一次,而且每个点都有个自己的状态,于是乎,我们似乎可以去用每个点从满状态跑到0状态来更新答案。一开始,每个点都是能经过其他所有状态的点,后来,每经过一个点便是少一种状态,利用这种方法,我们去求更新至0状态的时候的最短距离。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
//#define INF 10000007.
#define eps 1e-7
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 105;
int N, M, K, head[maxN], cnt, _UP, a[maxN];
struct Eddge
{
    int nex, to, val;
    Eddge(int a=-1, int b=0, int c=0):nex(a), to(b), val(c) {}
}edge[maxN * maxN];
inline void addEddge(int u, int v, int w)
{
    edge[cnt] = Eddge(head[u], v, w);
    head[u] = cnt++;
}
struct search
{
    int dis[maxN][1 << 13];
    bool inque[maxN][1 << 13];
    queue<pair<int, int>> Q;
    pair<int, int> now;
    inline int bfs()
    {
        int ans = INF, u, sta, v;
        for(int i=1; i<=N; i++) for(int j=0; j<_UP; j++) { dis[i][j] = INF; inque[i][j] = false; }
        for(int i=1; i<=N; i++) { dis[i][_UP ^ a[i]] = 0; Q.push(MP(i, _UP ^ a[i])); inque[i][_UP ^ a[i]] = true; }
        while(!Q.empty())
        {
            now = Q.front(); Q.pop();
            u = now.first; sta = now.second; inque[u][sta] = false;
            if(!sta)
            {
                ans = min(ans, dis[u][sta]);
                continue;
            }
            for(int i=head[u]; ~i; i=edge[i].nex)
            {
                v = edge[i].to;
                if(!(a[v] & sta)) continue;
                if(dis[v][sta ^ a[v]] > dis[u][sta] + edge[i].val)
                {
                    dis[v][sta ^ a[v]] = dis[u][sta] + edge[i].val;
                    if(!inque[v][sta ^ a[v]])
                    {
                        inque[v][sta ^ a[v]] = true;
                        Q.push(MP(v, sta ^ a[v]));
                    }
                }
            }
        }
        return ans;
    }
} Sear;
inline void init()
{
    cnt = 0; _UP = (1 << K) - 1;
    for(int i=1; i<=N; i++) head[i] = -1;
}
int main()
{
    scanf("%d%d%d", &N, &M, &K);
    init();
    for(int i=1; i<=N; i++) { scanf("%d", &a[i]); a[i] = 1 << a[i]; }
    for(int i=1, u, v, w; i<=M; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        addEddge(u, v, w);
    }
    int ans = Sear.bfs();
    if(ans == INF) printf("Ushio!\n");
    else printf("%d\n", ans);
    return 0;
}
发布了770 篇原创文章 · 获赞 927 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/104125960