zoukankan      html  css  js  c++  java
  • UVA 10537 The Toll! Revisited 最短路

    d[u]表示从u离开时最少需要多少才能达到要求。

    从终点开始往前更新,求出前继结点最少需要的d是多少

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    #define pb(a) push(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in1.txt","r",stdin);
        freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    
    struct Edge
    {
        int from,to,dist;
    };
    struct HeapNode
    {
        ll d;
        int u;
        bool operator < (const HeapNode &ant ) const
        {
            return d>ant.d;
        }
    };
    ll BS(ll x,int u)
    {
        if(u>=26)return x+1;
        ll l=0,r=LONG_LONG_MAX;
        while(l<r)
        {
            ll mid=(r+l)>>1;
            if(mid-(mid%20==0?mid/20:mid/20+1)>=x)
                r=mid;
            else l=mid+1;
        }
        return l;
    }
    const int maxn = 55;
    const int n=52;
    vector<int> g[maxn];
    vector<Edge> edge;
    int done[maxn];
    ll d[maxn];
    int p[maxn];
    int toid(char c)
    {
        if(c>='A'&&c<='Z')return c-'A';
        else return c-'a'+26;
    }
    char tochar(int id)
    {
        if(id<26)return id+'A';
        else return id-26+'a';
    }
    void init()
    {
        for(int i=0;i<n;i++)
            g[i].clear();
        edge.clear();
    }
    void add(int u,int v,int w)
    {
        Edge e=(Edge){u,v,w};
        edge.push_back(e);
        g[u].push_back(edge.size()-1);
    }
    void dijksta(int e,int need)
    {
        for(int i=0;i<n;i++)
            d[i]=LONG_LONG_MAX;
        memset(done,0,sizeof(done));
        priority_queue<HeapNode> q;
    
        d[e]=need;
        p[e]=-1;
        q.push((HeapNode){need,e});
    
        while(!q.empty())
        {
            HeapNode x=q.top();q.pop();
            int u=x.u;
            if(done[u])continue;
            done[u]=1;
            for(int i=0;i<g[u].size();i++)
            {
                int v=edge[g[u][i]].to;
                ll w=BS(d[u],u);
                if(w<d[v])
                {
                    d[v]=w;
                    p[v]=u;
                    q.push((HeapNode){d[v],v});
                }
            }
        }
    }
    int main()
    {
        int m;
        int ca=0;
        while(scanf("%d",&m)!=EOF&&m!=-1)
        {
            init();
            for(int i=1;i<=m;i++)
            {
                int u=toid(getch());
                int v=toid(getch());
                add(u,v,1);
                add(v,u,1);
            }
            int need;
            scanf("%d",&need);
            int s=toid(getch()),e=toid(getch());
            dijksta(e,need);
            ll res=d[s];
            printf("Case %d:
    ",++ca);
            printf("%lld
    ",res);
    
            int G[maxn][maxn]={0};
            for(int i=0;i<n;i++)
                for(int j=0;j<g[i].size();j++)
                    G[i][edge[g[i][j]].to]=1;
            for(int u=s;;)
            {
                printf("%c%c",tochar(u),u==e?'
    ':'-');
                if(u==e)break;
                for(int i=0;i<n;i++)
                {
                    if(G[u][i]&&d[u]==BS(d[i],i))
                    {
                        u=i;break;
                    }
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    逆向与BOF基础——注入shellcode并执行&Return-to-libc
    20145304 网络对抗技术 逆向与Bof基础
    20145303刘俊谦 《网络对抗》Exp9 Web安全基础实践
    操作系统取证实践
    20145303刘俊谦 Exp8 Web基础
    20145303刘俊谦 Exp7 网络欺诈技术防范
    20145303《网络对抗》信息收集和漏洞扫描技术
    20145303刘俊谦《网络攻防》Exp4 Msf基础
    msf辅助模块的应用
    Adobe漏洞攻击
  • 原文地址:https://www.cnblogs.com/BMan/p/3632922.html
Copyright © 2011-2022 走看看