HDU

来源:互联网 发布:最全英语单词数据库 编辑:程序博客网 时间:2024/06/05 04:00

FFF at Valentine


Problem Description

At Valentine's eve, Shylock and Lucar were enjoying their time as any other couples. Suddenly, LSH, Boss of FFF Group caught both of them, and locked them into two separate cells of the jail randomly. But as the saying goes: There is always a way out , the lovers made a bet with LSH: if either of them can reach the cell of the other one, then LSH has to let them go.
The jail is formed of several cells and each cell has some special portals connect to a specific cell. One can be transported to the connected cell by the portal, but be transported back is impossible. There will not be a portal connecting a cell and itself, and since the cost of a portal is pretty expensive, LSH would not tolerate the fact that two portals connect exactly the same two cells.
As an enthusiastic person of the FFF group, YOU are quit curious about whether the lovers can survive or not. So you get a map of the jail and decide to figure it out.
 

Input
Input starts with an integer T (T≤120), denoting the number of test cases.
For each case,
First line is two number n and m, the total number of cells and portals in the jail.(2≤n≤1000,m≤6000)
Then next m lines each contains two integer u and v, which indicates a portal from u to v.
 

Output
If the couple can survive, print “I love you my love and our love save us!”
Otherwise, print “Light my fire!”
 

Sample Input
35 51 22 32 43 54 53 31 22 33 15 51 22 33 13 44 5
 

Sample Output
Light my fire!I love you my love and our love save us!I love you my love and our love save us!
 



题意:给定一个有向图,求任意两点之间是否可达……


解题思路:暴力求出两点间的最短路径,这里要注意假设两点为i,j,i不能到j,但j有可能到i……迪杰斯特拉会超时……用SPFA,或者用官方题解的方法……

1005

缩点为DAG,则如果有两个及以上出度为0的点则有不能走到同一个点的点对。


#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>#define inf 1<<29#define MAXV 6005using namespace std;struct edge{    int v1,v2,w,next;}e[MAXV];int n,m,edge_num;int head[MAXV],d[MAXV];bool vis[MAXV];bool ans[1005][1005];void insert_edge(int v1,int v2,int w){    e[edge_num].v1=v1;    e[edge_num].v2=v2;    e[edge_num].w=w;    e[edge_num].next=head[v1];    head[v1]=edge_num++;}void spfa(int en){    queue<int> que;    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)        d[i]=inf;    que.push(en);    d[en]=0;    vis[en]=1;    while(!que.empty()){        int tp=que.front();        que.pop();        vis[tp]=0;        for(int i=head[tp];i!=-1;i=e[i].next)            if(d[tp]+e[i].w<d[e[i].v2]){                d[e[i].v2]=d[tp]+e[i].w;                if(!vis[e[i].v2]){                    vis[e[i].v2]=1;                    que.push(e[i].v2);                }            }    }    for(int i=1;i<=n;i++){        if(d[i]==inf)            ans[en][i]=false;        else            ans[en][i]=true;    }}int main(){    int a,b;    int t;    scanf("%d",&t);    while(t--){        scanf("%d%d",&n,&m);        edge_num=0;        memset(head,-1,sizeof(head));        for(int i=1;i<=m;i++){            scanf("%d%d",&a,&b);            insert_edge(a,b,1);        }        bool flag=true;        for(int i=1;i<=n;i++){            spfa(i);        }        for(int i=1;i<=n;i++){            if(!flag)                break;            for(int j=i+1;j<=n;j++){                if(!(ans[i][j]||ans[j][i])){                    flag=false;                    break;                }            }        }        if(!flag)            printf("Light my fire!\n");        else            printf("I love you my love and our love save us!\n");    }    return 0;}