2013
12-26

# Sightseeing tour

The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it’s possible to construct a sightseeing tour under these constraints.

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1<=m<=200, 1<=s<=1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1<=xi,yi<=m, 0<=di<=1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it’s a two-way street. You may assume that there exists a junction from where all other junctions can be reached.

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1<=m<=200, 1<=s<=1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1<=xi,yi<=m, 0<=di<=1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it’s a two-way street. You may assume that there exists a junction from where all other junctions can be reached.

4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0

possible
impossible
impossible
possible

//DFS+剪枝优化
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm>
using namespace std;
int numbers[25];
bool used[25];
int side;
int num;
bool cmp(int a,int b)
{
return a>b;
}
bool dfs(int tsum,int cur,int leave)    //tsum:这个边已经组了多长；cur：正在组的是第几条边；leave:组这条边已经用了几根棒
{
if(tsum==side)
{
cur++;
tsum=0;
}
if(cur==4)return true;

for(int i=leave; i<num; i++)   //i从leave开始。因为对棒已经降序排过序了，再搜索只需要对leave之后的棒进行搜索，它之前的肯定是不会符合的。
{
if(i&&numbers[i]==numbers[i-1]&&!used[i-1])//相邻的相等的前者没有加入，则这个也不行。
continue;

if(used[i]==0)
{
if(tsum+numbers[i]<side)
{
used[i]=1;
if(dfs(tsum+numbers[i],cur,i+1))return true; //搜索的起始点很重要。i+1传入了下层递归搜索的起始点
used[i]=0;
}
if(tsum+numbers[i]==side)
{
used[i]=1;
if(dfs(0,cur+1,0))
return true;

used[i]=0;
}
if(tsum==0)break;
}
}

return false;
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int sum=0;
scanf("%d",&num);
for(int i=0; i<num; i++)
{
scanf("%d",&numbers[i]);
sum+=numbers[i];
}
if(sum%4!=0)   //从全局剪枝。
{
printf("no\n");
continue;
}
side=sum/4;
sort(numbers,numbers+num,cmp);  //按降序排序。这样保证了只需要在搜索的起始点后面找棒，起始点前面的棒就不用再搜了。
if(numbers[0]>(side)) //从全局剪枝。
{
printf("no\n");
continue;
}

memset(used,0,sizeof(used));

if(dfs(0,1,0))
{
printf("yes\n");
}
else printf("no\n");

}
return 0;
}

1. 5.1处，反了；“上一个操作符的优先级比操作符ch的优先级大，或栈是空的就入栈。”如代码所述，应为“上一个操作符的优先级比操作符ch的优先级小，或栈是空的就入栈。”

2. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术，也不懂什么是算法，从而也不要求程序员懂什么算法，做程序从来不考虑性能问题，只要页面能显示出来就是好程序，这是国内的现状，很无奈。