Made In Heaven

//该题与poj2449 基本相同 

Made In Heaven_第1张图片

Made In Heaven_第2张图片

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int vis[1005];
int low[1005];
int head[1005],head1[1005];
int from,to;
int tot;
int n,m,k;
struct edge
{
    int u,v;
    int len;
    int next;       //正向
    int next1;      //反向
    edge() {}
    edge(int x,int y,int c):u(x),v(y),len(c) {}
};
edge p[100005];

struct node
{
    int po;
    int g,h;
    node() {}
    node(int x,int y,int z):po(x),g(y),h(z) {}
    bool operator<(const node& a)const
    {
        return g+h>a.g+a.h;
    }
};

void add_edge(int u,int v,int c)
{
    p[tot] = edge(u,v,c);
    p[tot].next = head[u];
    head[u] = tot;
    p[tot].next1 = head1[v];
    head1[v] = tot++;
}

void  work(int c)       //从to开始找出所有点到to的最短路
{
    priority_queueque;
    memset(vis,0,sizeof(vis));
    for(int i = 1; i <= n; i++)
        low[i] = INF;
    low[c] = 0;
    que.push(node(c,0,0));
    while(!que.empty())
    {
        node cur = que.top();
        que.pop();
        if(vis[cur.po])
            continue;
        vis[cur.po] = 1;
        for(int i = head1[cur.po]; ~i; i = p[i].next1)       //反向查找
        {
            if(low[p[i].u] > low[cur.po]+p[i].len)
            {
                low[p[i].u] = low[cur.po] + p[i].len;
                que.push(node(p[i].u,0,low[p[i].u]));
            }
        }
    }
}


int bfs(int t)
{
    int num = 0;
    priority_queueque;
    node cur;
    cur.po = t;
    cur.g = 0;
    que.push(cur);
    while(!que.empty())
    {
        cur = que.top();
        que.pop();
        int tp = cur.po;
        if(tp == to)
        {
            num ++;
            if(num == k)
            {
                return cur.g;
            }
        }
        for(int i = head[cur.po]; ~i; i = p[i].next)
        {
            node tmp;
            int x = p[i].v;
            tmp.po = x;
            tmp.g = cur.g + p[i].len;
            tmp.h = low[x];
            que.push(tmp);
        }
    }
    return -1;
}

void ini()
{
    tot = 0;
    memset(head,-1,sizeof(head));
    memset(head1,-1,sizeof(head1));
}

int main()
{
    int a,b,len, t;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        ini();
        scanf("%d%d%d%d",&from,&to,&k, &t);
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d",&a,&b,&len);
            add_edge(a,b,len);
        }

        work(to);
        if(low[from] == INF)   //如果from不能到达to
        {
            printf("Whitesnake!\n");
            continue;
        }
        if(from == to)
        {
            k++;
        }
        printf("%s\n",(bfs(from)<=t) ? "yareyaredawa" :"Whitesnake!");
    }
    return 0;
}

 

你可能感兴趣的:(计蒜客)