HDU 1317 XYZZY【Bellman_Ford判断正环】

题意:给出n个房间,初始在房间1有100的能量值,每次进入一个房间,能量值可能增加也可能减小,(是点权,不是边权),问能否到达终点的时候能量值还为正

这题自己写的时候wa--wa--

后来看了题解,还是wa---wa---

题解很详细http://blog.csdn.net/freezhanacmore/article/details/9937327

记录下自己犯的错误吧

首先是floyd函数初始化的时候,直接写在了函数里面,这样是不对的,因为输入值在前,调用函数在后,这样就相当于将之前输入的可能部分改变了(这个改了好久啊-----55555)

还有就是Bellman_ford时,因为是求正环,所以是d[i][j]=-INF(i!=j)

还有就是输入的权值是点权

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include <cmath>
 5 #include<stack>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<algorithm>
11 using namespace std;
12
13 typedef long long LL;
14 const int INF = 10000000000;
15 const int mod=1000000007;
16 const int maxn=110;
17 int en[maxn],d[maxn],w[maxn][maxn];
18 int n,m,ecnt;
19
20
21 struct Edge{
22     int u,v;
23 } e[maxn*maxn];
24
25 void add(int u,int v){
26     e[++ecnt].u=u;
27     e[ecnt].v=v;
28 }
29
30 void floyd(){
31     for(int k=1;k<=n;k++)
32      for(int i=1;i<=n;i++)
33       for(int j=1;j<=n;j++)
34       w[i][j]=w[i][j]||(w[i][k]&&w[k][j]);
35 }
36
37
38 int Bellman_ford(){
39     for(int i=1;i<=n;i++) d[i]=-INF;
40     d[1]=100;
41
42     for(int i=1;i<n;i++){
43         for(int j=1;j<=ecnt;j++){
44             int v=e[j].v;
45             int u=e[j].u;
46             if(d[v]<d[u]+en[v]&&d[u]+en[v]>0){
47                 d[v]=d[u]+en[v];
48             }
49         }
50     }
51
52     for(int j=1;j<=ecnt;j++){
53             int v=e[j].v;
54             int u=e[j].u;
55             if((d[v]<d[u]+en[v])&&d[u]+en[v]>0) {
56                 if(w[v][n])
57                 return 1;
58             }
59     }
60     return d[n]>0;
61 }
62
63 int main(){
64     int num;
65     while(scanf("%d",&n)!=EOF&&n!=-1){
66         ecnt=0;
67
68         memset(en,0,sizeof(en));
69         memset(w,0,sizeof(w));
70         for(int i=1;i<=n;i++) w[i][i]=1;
71         for(int i=1;i<=n;i++){
72             scanf("%d %d",&en[i],&num);
73             while(num--){
74                 int u;
75                 scanf("%d",&u);
76                 w[i][u]=1;
77                 add(i,u);
78             }
79         }
80
81         floyd();
82         if(Bellman_ford()) printf("winnable\n");
83         else printf("hopeless\n");
84     }
85     return 0;
86 }

加油啊- go---go--go

时间: 2024-12-05 04:07:05

HDU 1317 XYZZY【Bellman_Ford判断正环】的相关文章

HDU 1317 XYZZY(floyd+bellman_ford判环)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始能量有100,行走的途中能量不能小于等于0. 思路: 首先我们用floyd来判断一下1和n之间是否有通路. 其次就是bellman_ford算法来判正环了. 1 #include <iostream> 2 #include <cstring> 3 #include <algori

hdu 1317 XYZZY(spfa判环)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 大致题意:有n个房间,每个房间都有对应的能量值(可正可负),现在从1出发要到达n,初始能量为100,问是否能够达到n点,到达n的条件是中间及最后的能量值都要大于0. 思路:若不考虑环,那么求最长路判断是否大于0即可.若存在负环,对求最长路也没影响:但当存在正环时,最长路就不存在了.可用spfa判断,当某点入队超过n次,那么它必定在环中,直接将其dis置为INF,并不再将其近队列.最后若能到达n则可行,否

HDU 1317 XYZZY

题意是指 从1 到 N 能否保证 到达每个点的时候 能量都为正数. 起点 1 初始100 点能量. 输入是 从 1 ~ N , 分别是 能量,能到m个房间, 分别是 a1,a2,a3,-,am 可以给每个能到达的点 而 产生的边赋权,即能量值. SPFA 求最长路的变形,出现负环不怕,出现正环就需要一点改动. vis[]标记是否需要入队,d[] 表示能量,que[] 表示入队次数. 如果出现正环(que[v]>=n),表明一定能 保证到达每个点的时候都是正能量. 这时候直接 将 d[v] 赋最大

POJ 1860 Currency Exchange(如何Bellman-Ford算法判断正环)

题目链接: https://cn.vjudge.net/problem/POJ-1860 Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be s

Currency Exchange POJ - 1860 spfa判断正环

//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace std; const int N=1e4; const int INF=2e9; int h[N],to[N],ne[N],idx; double r[N],c[N]; int n, m,X; double V; void add(int u,int v,double r1,double c1) { to[id

HDU 1317(Floyd判断连通性+spfa判断正环)

XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4421    Accepted Submission(s): 1252 Problem Description It has recently been discovered how to run open-source software on the Y-Crate gami

HDU 1317 XYZZY (SPFA 找正环 + Floyd 判连通)

XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3004    Accepted Submission(s): 817 Problem Description It has recently been discovered how to run open-source software on the Y-Crate gami

HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5304    Accepted Submission(s): 1510 Problem Description It has recently been discovered how to run open-source software on the Y-Crate gam

hdu 1317 XYZZY 国产SPFA算法AC,,题目输入部分真特么难懂。。内有解析

XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3017    Accepted Submission(s): 824 Problem Description It has recently been discovered how to run open-source software on the Y-Crate gami