You have been hired by the quality control division of a world famous board game company. Their creative and design division comes up, on a daily basis, with great ideas for board games, but sometimes the scoring of the proposed games does not match the story board or leads the player to impossible or undesirable situations.
Most of the games this company produces can be described loosely as race games. Race games are games where the players need to go from an initial square to a final square, performing along the way, a series of moves, gaining or losing score points for each of those moves. Moves can be influenced by player's decisions, drawing of cards, rolling of dices, etc.
Your task is to check the description of a given game, stating the lowest possible score, or if it can lead to an infinitely high (there's no way the player can win the game), or to an infinitely low score.
Input
A positive integer P in a single line followed by a sequence of P game descriptions. The first line of the game description contains a positive integer N not greater than 300, indicating the number of squares in the game. The second line contains two non-negative integers, I and F, defining the initial and final squares for this game, where I and F are lesser than N. The third line contains an integer M, indicating the number of possible moves of the game. The following M lines describe all the possible moves of the game. Each line, describing one possible move, consists of three integers, respectively, the initial square and final square of the move, in the range [0, N-1],and the corresponding score.
Output
The output consists of a single line with an integer, indicating the lowest possible score for the proposed game. If the scores are infinitely high or low then your program should output infinity.
Sample Input
2 4 0 3 4 0 1 5 0 2 7 2 1 -3 1 3 2 4 0 3 3 0 1 5 0 2 7 2 1 -3
Sample Output
6 infinity
AC代码:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#define MAX 100010
using namespace std;
struct NODE
{
int v;
long long w;
};
vector<NODE>edge[MAX];
queue<int>stk;
int n,ncase,m;
int beg,end,vis[MAX];
long long dis[MAX];
int cnt[MAX];
int spfa()
{
memset(cnt,0,sizeof(cnt));
memset(vis,0,sizeof(vis));
for(int i=0;i<n;++i)
dis[i]=((long long )(1))<<61; //莫名其妙的赋值
while(!stk.empty())
stk.pop();
stk.push(beg);
vis[beg]=1;
dis[beg]=0;
while(!stk.empty())
{
int u=stk.front( );
stk.pop();
vis[u]=0;
for(int i=0;i<edge[u].size();++i)
{
int v=edge[u][i].v;
long long w=edge[u][i].w;
if(dis[v]-dis[u]>w)
{
dis[v]=dis[u]+w;
if(!vis[v])
{
stk.push(v);
vis[v]=1;
if(++cnt[v]>n)
return 0;
}
}
}
}
return 1; }
int main(void)
{
scanf("%d",&ncase);
int u;
while(ncase--)
{
scanf("%d",&n);
scanf("%d%d",&beg,&end);
scanf("%d",&m);
for(int i=0;i<n;++i)
edge[i].clear();
NODE nd_tmp;
while(m--)
{
scanf("%d%d%lld",&u,&nd_tmp.v,&nd_tmp.w);
edge[u].push_back(nd_tmp);
}
int ans=spfa();
if(dis[end]==((long long) (1))<<61|| !ans)
printf("infinity\n");
else
printf("%d\n",dis[end]);
}
return 0;
}
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3033