10557  XYZZY
Moderator: Board moderators

 Experienced poster
 Posts: 131
 Joined: Thu Apr 17, 2003 8:39 am
 Location: Baku, Azerbaijan
10557  XYZZY
Hi people. I get WA. My algo is so:
First I build graph where g[i,j] = energy[j] (energy of the second room)
The I run Floyd algo and find maximal energy (weight) between all rooms.
If there is cycle where energy>0 then we can always win: if g[i,i]>0;
we can always win because we can continue cycling as much as we need.
If there is no cycle then we look at g[1,n] the energy between 1st and last rooms. If this enery>100 then we win and lose if we cannot win.
What is wrong with this algo?
First I build graph where g[i,j] = energy[j] (energy of the second room)
The I run Floyd algo and find maximal energy (weight) between all rooms.
If there is cycle where energy>0 then we can always win: if g[i,i]>0;
we can always win because we can continue cycling as much as we need.
If there is no cycle then we look at g[1,n] the energy between 1st and last rooms. If this enery>100 then we win and lose if we cannot win.
What is wrong with this algo?
_____________
NO sigNature
NO sigNature
are u sure that Floyd can handle negative edges/ cycles?
and u wrote:
and u wrote:
what if the cycle doesn't lead to the destination? Try this case (i found it on the Web):If there is cycle where energy>0 then we can always win
Output should be:7
0 1 2
0 2 3 5
100 1 4
100 1 7
1 1 6
0 1 5
0 0
Hope it helpshopeless
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00  16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00  16:00 (UTC)
URL: http://uva.onlinejudge.org

 Experienced poster
 Posts: 131
 Joined: Thu Apr 17, 2003 8:39 am
 Location: Baku, Azerbaijan
Thank you for your help. I used it. I am sure about negatives.
My floyd is:
[pascal]for k:=1 to n do
for i:=1 to n do
for j:=1 to n do
if connected[i,k] and connected[k,j] then
begin
if not connected[i,j] then
begin
d[i,j]:=d[i,k]+d[k,j];
connected[i,j]:=true;
end else
begin
if d[i,j]<d[i,k]+d[k,j] then d[i,j]:=d[i,k]+d[k,j];
end;
end;[/pascal]
Now I check for if we can reach destination from cycle and check if cycle is reachable from sourse:
[pascal]for i:=1 to n do
if (d[i,i]>0)and(connected[1,i])and(connected[i,n]) then win:=true;
if d[1,n]>100 then win:=true;[/pascal]
For your testcase I get "hopeless". But I still get WA.
My floyd is:
[pascal]for k:=1 to n do
for i:=1 to n do
for j:=1 to n do
if connected[i,k] and connected[k,j] then
begin
if not connected[i,j] then
begin
d[i,j]:=d[i,k]+d[k,j];
connected[i,j]:=true;
end else
begin
if d[i,j]<d[i,k]+d[k,j] then d[i,j]:=d[i,k]+d[k,j];
end;
end;[/pascal]
Now I check for if we can reach destination from cycle and check if cycle is reachable from sourse:
[pascal]for i:=1 to n do
if (d[i,i]>0)and(connected[1,i])and(connected[i,n]) then win:=true;
if d[1,n]>100 then win:=true;[/pascal]
For your testcase I get "hopeless". But I still get WA.
_____________
NO sigNature
NO sigNature

 Experienced poster
 Posts: 131
 Joined: Thu Apr 17, 2003 8:39 am
 Location: Baku, Azerbaijan
gvcormac what do you mean?
connected is initialized as, if there is a door from this room to other then connected[this room, other room]:=true;
And it corresponds to the problem statement I think. What is not OK? Why I get WA? Isn't my algo right. Can anyone give example where I am wrong.
Thanx in advance.
connected is initialized as, if there is a door from this room to other then connected[this room, other room]:=true;
And it corresponds to the problem statement I think. What is not OK? Why I get WA? Isn't my algo right. Can anyone give example where I am wrong.
Thanx in advance.
_____________
NO sigNature
NO sigNature

 Experienced poster
 Posts: 131
 Joined: Thu Apr 17, 2003 8:39 am
 Location: Baku, Azerbaijan
Here is my code.Could you tell me what's wrong in it??
Code: Select all
#include<iostream>
#include<fstream>
#include<queue>
#include<memory.h>
using namespace std;
ifstream fin("10557.in");
int eg[101][101];
bool way[101][101];
bool go(int n)
{
int i,j,k;
if(n==1)return true;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(!way[j][i])continue;
for(k=0;k<n;k++)
{
if(way[i][k])
{
if(eg[j][k]<eg[j][i]+eg[i][k]  !way[j][k])
{
eg[j][k]=eg[j][i]+eg[i][k];
}
way[j][k]=1;
}
}
}
}
if(way[0][n1])
{
if(eg[0][n1]>100)return true;
for(i=0;i<n;i++)
if(eg[i][i]>0 && (way[0][i] && way[i][n1]) && eg[0][i]>100)
return true;
}
return false;
}
int main()
{
int i,j,n,m;
while(fin>>n && n>=0)
{
int a,b;
memset(way,0,sizeof(way));
memset(eg,0,sizeof(eg));
for(i=0;i<n;i++)
{
fin>>a>>m;
for(j=0;j<m;j++)
{
fin>>b;b;
eg[i][b]=a;
way[i][b]=1;
}
}
if(go(n) && n)
cout<<"winnable\n";
else
cout<<"hopeless\n";
}
return 0;
}

 Experienced poster
 Posts: 131
 Joined: Thu Apr 17, 2003 8:39 am
 Location: Baku, Azerbaijan
Floyd algo
Lain why the answer is hopeless???
From room 0 we go to room 4 then to 3 then to 2 and then to 5.
From room 0 we go to room 4 then to 3 then to 2 and then to 5.
 [x; y]
Where x is room number and y is current energy
________90_________80_______+100________0
[0; +100] > [4; +10] > [3; 70] > [2; +30] > [5; +30] and YOU WIN
_____________
NO sigNature
NO sigNature
Farid:
May be I misundrestood the problem, but there is said that:
May be I misundrestood the problem, but there is said that:
And when you enters the room 3 you have negative energy(running out of energy). That's why it is hopeless. Is it wrong?This process continues until she wins by entering the finish room or dies by running out of energy (or quits in frustration).

 Experienced poster
 Posts: 131
 Joined: Thu Apr 17, 2003 8:39 am
 Location: Baku, Azerbaijan