HDU(1572),最短路,DFS
生活随笔
收集整理的這篇文章主要介紹了
HDU(1572),最短路,DFS
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1572
很久沒寫深搜了,有點(diǎn)忘了。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAXN 33 #define inf 1<<30 int map[MAXN][MAXN]; int n,MIN,k; int num[MAXN]; bool mark[MAXN];void dfs(int start,int count,int dist) {if(count==k){MIN=min(dist,MIN);return ;}for(int i=1; i<=k; i++){if(!mark[i]){mark[i]=true;dfs(num[i],count+1,dist+map[start][num[i]]);mark[i]=false;}} }int main() {int _case;while(scanf("%d",&n)&&n){for(int i=0; i<n; i++)for(int j=0; j<n; j++)scanf("%d",&map[i][j]);k=0,MIN=inf;scanf("%d",&_case);memset(mark,false,sizeof(mark));while(_case--){int x;scanf("%d",&x);if(mark[x])continue;mark[x]=true;num[++k]=x;}memset(mark,false,sizeof(mark));dfs(0,0,0);printf("%d\n",MIN);}return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/TreeDream/p/5740167.html
總結(jié)
以上是生活随笔為你收集整理的HDU(1572),最短路,DFS的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: angularjs 工具方法
- 下一篇: [reprint]如何编写引导程序 He