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
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!”
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;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 0-1背包问题 DPL_1_B- 0-1 Knapsack Problem
- 动态规划(树形DP)之Anniversary party
- 【Log历练手册】Frame must be terminated with a null octet
- Android:仿苹果按钮
- [Python] wxPython 编辑框组件学习总结 (原创)
- HDU
- java程序猿的日常(每天更新)
- 招聘面试的套路与原则
- 连续子数组的最大和
- float和double存储方式
- 数据结构 图
- js,jq生成json数据
- Linux下安装ffmpeg
- 配置篇 04. 硬盘分区 ❀ Windows Server 2016