HDU 1142 A Walk Through the Forest
来源:互联网 发布:淘宝金酷娃消防车视频 编辑:程序博客网 时间:2024/06/05 02:40
记忆化搜索+dfs
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;int n,m;int dir[4][2]={1,0,0,1,-1,0,0,-1};int map[1005][1005];bool visit[1005];int dist[1005];int sum[1005];const int INF=0x7fffff;struct node{ int x,y;};queue<int> q;void BFS(){ int i,now; memset(visit,false,sizeof(visit)); for(i=0;i<=n;i++) dist[i]=INF; while(!q.empty()) q.pop(); dist[2]=0; q.push(2); visit[2]=true; while(!q.empty()) { now=q.front(); q.pop(); visit[now]=false; for(i=1;i<=n;i++) { if(dist[i]>dist[now]+map[now][i]) { dist[i]=dist[now]+map[now][i]; if(visit[i]==false) { q.push(i); visit[i]=true; } } } }}int dfs(int now){ int i,sum1=0; if(now==2) return 1; if(sum[now]) return sum[now]; for(i=1;i<=n;i++) { if(map[now][i]<INF&&dist[now]>dist[i]) { if(sum[i]) sum1+=sum[i]; else sum1+=dfs(i); } } sum1+=sum[now]; sum[now]=sum1; return sum[now];}int main(){ while(scanf("%d",&n)!=EOF&&n) { memset(sum,0,sizeof(sum)); scanf("%d",&m); int s,t,v; for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } for(int i=0;i<m;i++) { scanf("%d%d%d",&s,&t,&v); if(v<map[s][t]) map[s][t]=map[t][s]=v; } BFS(); // for(int i=0;i<=n;i++) cout<<dist[i]<<' '; // cout<<endl; int sum1=dfs(1); printf("%d\n",sum1); } return 0;}
- HDU 1142 A Walk Through the Forest
- A Walk Through the Forest HDU 1142
- hdu 1142 A Walk Through the Forest
- Hdu-1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- iOS开发之 Json解析的四种方法
- 深入研究 C++中的 STL Deque 容器
- CF197#DIV2:A-Helpful Maths
- 前端Web服务器+后台MySQL数据库+NFS服务
- B. Ciel and Flowers
- HDU 1142 A Walk Through the Forest
- 【rzxt】教你如何删除注册表的U盘使用记录
- android 蓝牙模块遇到的问题,关闭蓝牙以后下次不能正常连接
- Test
- CSS3之选择器
- VS2012:error LNK2026: 模块对于 SAFESEH 映像是不安全的
- dbca silent oraclle 11g R2 RAC 创建数据库试验
- WS_DELIVERY_UPDATE 修改数量、过账日期并发货过账
- Cas Password + Salt修改说明