比赛 难度范围:提高至省选 评测结果 AAAAAAATAA
题目名称 荒野迷路 最终得分 90
用户昵称 HouJikan 运行时间 4.317 s
代码语言 C++ 内存使用 5.19 MiB
提交时间 2014-10-16 19:20:04
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int INF=0x7ffffff;
//==============struct declaration==============
struct adj{
  int to,dist;
  adj (int to=0,int dist=0):to(to),dist(dist){}
};
//==============var declaration=================
const int MAXN=510;
bool win[MAXN][5010];
bool vis[MAXN][5010]; 
vector <adj> Edge[MAXN];
int n,m,k,T;
//==============function declaration============
bool drive(int x,int fuel);
//==============main code=======================
int main()
{  
  string FileName="fieldlost";//程序名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
  scanf("%d",&T);
  while (T--){
    scanf("%d%d%d",&k,&n,&m);
    For(i,1,n) Edge[i].clear();
    For(i,1,m){
      int s,e,len;
      scanf("%d%d%d",&s,&e,&len);
      Edge[s].push_back(adj(e,len));
      Edge[e].push_back(adj(s,len));
    }
    memset(vis,false,sizeof(vis));
    if (drive(1,k))
      printf("Yes!\n");
    else 
      printf("No!\n");
  }
  return 0;
}
//================fuction code====================
bool drive(int x,int fuel)//目前处于x,有fuel油,先开能否获胜 
{
  if (vis[x][fuel])
    return win[x][fuel];
  bool suc=false;
  int siz=Edge[x].size()-1;
  For(i,0,siz){
    adj &e=Edge[x][i];
    if (e.dist<=fuel){
      suc|=(!drive(e.to,fuel-e.dist));
    }
  }
  vis[x][fuel]=true;
  return win[x][fuel]=suc;
}