其他分享
首页 > 其他分享> > 分层图洛谷P5122. Fine Dining G

分层图洛谷P5122. Fine Dining G

作者:互联网

今天就completely在划水,mentor也不搭理了

那就做题吧,下午做gre的题,这周六过生日2333

上次有个分层图的题其实一直没想明白,但我现在这题就搞明白了

这题问的是要求是否能吃到艹,我本来想用费用流,但是数据量就死心了

然后看了看是用dij跑的

分层图可以解决图中一些存在着某些不等式关系图

比如这个 有dist(u) + dist(w) <= dist(n) + x

那么整理一下就有 dist(u) + dist(w) - n <= dist(n)

化成了一个数学公式,所以我们先取一遍n到各个点的最短路,那么是NlogN的

然后连边,然后超级原点,向这些有艹剁点进行连边,费用是 dist(u, w) - x, x是费用,然后跑一边看到新的原点n始否能够小于单纯的dist(n)

代码:

#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
#define limit (2000000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
#define debug(x) cout<<x<<endl
typedef long long ll;
typedef unsigned long long ull;
char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
inline ll read(){
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;char s = getchar();
    while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
    while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if(x/ 10) print(x / 10);
    *O++=x % 10+'0';
}
void write(ll x, char c = 't') {
    if(x < 0)putchar('-'),x = -x;
    print(x);
    if(!isalpha(c))*O++ = c;
    fwrite(obuf,O-obuf,1,stdout);
    O = obuf;
}
int kase;
int n, m,k;
int a[limit];
int head[limit], cnt;
int dist[limit], vis[limit];
void init(){
    cnt = 0;
    memset(head, -1 , sizeof(head));
    memset(dist, INF, sizeof(dist));
    memset(vis, 0, sizeof(vis));
}
struct node{
    int to, w, next;
}edge[limit<<1];
void add(int u, int v, int w = 0){
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

struct nod{
    int to, w;
    bool operator<(const nod & rhs)const{
        return w > rhs.w;
    }
};
void dijkstra(int vs = 1){
    priority_queue<nod>q;
    q.push({vs,0});
    dist[vs] = 0;
    while(!q.empty()){
        nod u = q.top();
        q.pop();
        vis[u.to] = 1;
        for(int i  = head[u.to] ; ~i ; i = edge[i].next){
            int v = edge[i].to, w = edge[i].w;
            if(vis[v])continue;
            if(dist[u.to] + w < dist[v]){
                dist[v] = dist[u.to] + w;
                q.push({v, dist[v]});
            }
        }
    }
}

void solve(){
    n = read(), m = read(), k = read();
    init();
    struct backup{
        int x,y,w;
    };
    vector<backup>v;
    v.reserve(m);
    rep(i,1,m){
        int x = read(), y = read(), w = read();
        add(x,y,w);
        add(y,x,w);
        v.push_back({x,y,w});
    }

    dijkstra(n);//求出来n到所有的距离
    rep(i,1,n){
        a[i] = dist[i];
    }
    int ve = 5e5+1;
    rep(i,1,k){
        int x = read(), t = read();
        add(ve , x, dist[x] - t);
        //add(x,ve,dist[x] - t);
    }
    memset(dist, INF, sizeof(dist));
    memset(vis, 0,sizeof(vis));
    dijkstra(ve);
    rep(i,1,n - 1){
        if(dist[i] <= a[i]) puts("1");
        else puts("0");
    }

}
int32_t main() {
#ifdef LOCAL
    FOPEN;
    //FOUT;
#endif
    //FASTIO
    //cin>>kase;
    //while (kase--)
        solve();
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
    return 0;
}

 

标签:dist,int,rep,P5122,Dining,edge,read,Fine,define
来源: https://www.cnblogs.com/tiany7/p/14845005.html