1456 只会穷举 超时
http://acm.zju.edu.cn/show_problem.php?pid=1456
// 超时
// 这种题目不穷举怎么解?
#include <iostream.h>
int stack[100],top;
int temp[100],len=100;
int mincost=100000,cost;
int map[100][100];
int N;
int passed[100];
int tax[100];
int t;
void Memcpy()
{
int i;
for(i=0;i<100;i++)
temp[i]=stack[i];
}
void save_min()
{
if(cost<mincost)
{
mincost=cost;
len=top;
Memcpy();
}
if(cost==mincost)
{
if(len>top)
{
len=top;
Memcpy();
}
}
}
void print(int f)
{
int i;
temp[len++]=t;
cout<<"From "<<f<<" to "<<t<<" :"<<endl;
cout<<"Path: "<<temp[0];//-->5-->4-->3
for(i=1;i<len;i++)
{
cout<<"-->"<<temp[i];
}
cout<<endl;
cout<<"Total cost : "<<mincost<<endl;
cout<<endl;
}
void trv(int cur)
{
int c;
for(c=1;c<=N;c++)
{
if(!passed[c]&&map[cur][c]!=-1)
{
if(c==t)
{
cost=cost+map[cur][c];
save_min();
cost=cost-map[cur][c];
continue;
}
stack[top++]=c;
cost=cost+map[cur][c]+tax[c];
passed[c]=1;
trv(c);
passed[c]=0;
cost=cost-map[cur][c]-tax[c];
top--;
}
}
}
void clear()
{
int i;
for(i=0;i<100;i++)
{
passed[i]=0;
}
cost=0;
mincost=100000;
top=0;
len=100;
}
int main()
{
int i,j,f;
while(cin>>N&&N!=0)
{
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
cin>>map[i][j];
for(i=1;i<=N;i++)
cin>>tax[i];
while(cin>>f>>t&&(f!=-1&&t!=-1) )
{
clear();
passed[f]=1;
stack[top++]=f;
trv(f);
print(f);
}
}
return 0;
}