链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3033
源代码:
#include <iostream>
#include <cstdio>
#include <limits.h>
#define N 310
#define inf ( (long long)(1) )<<62
#define LL long long
using namespace std;
struct node
{
int sta,end;
LL val;
}map[N*N];
LL dist[N];
int n;
void bellman( int from,int to,int k )
{
int i,j,a,b;
bool in;
LL ori;
for( i=0;i<=n;i++ )
dist[i]=inf;
dist[from]=0;
for( i=0;i<n;i++ )
{
in=false;
for( j=0;j<k;j++ )
{
a=map[j].sta,b=map[j].end;
if( dist[a]!=inf && dist[b]>dist[a]+map[j].val )
{
dist[b]=dist[a]+map[j].val;
in=true;
}
}
if( !in ) break;
}
if( i==n || dist[to]==inf ) printf( "infinity\n" );
else printf( "%lld\n",dist[to] );
}
int main()
{
int T,m,i,j,sta,end;
int a,b;
LL value;
scanf( "%d",&T );
while( T-- )
{
scanf( "%d%d%d%d",&n,&sta,&end,&m );
for( i=0;i<m;i++ )
scanf( "%d%d%lld",&map[i].sta,&map[i].end,&map[i].val );
bellman( sta,end,m );
}
//system("pause");
return 0;
}