hdu1142
来源:互联网 发布:天津大学网络继续教育 编辑:程序博客网 时间:2024/06/06 00:50
这道题纠结了好久~~~最后发现最短路的算法求错了~~~
可是以前用此代码AC了好几道题了~~~汗~~求指点~
先是最后ac代码:
#include <iostream>#include <cstdio>#include <queue>#include <cstring>#define INF 1000010010using namespace std;int d[1005];int vis[1005];int w[1005][1005];int s[1005];void init(int n){ memset(s,0,sizeof(s)); memset(vis,0,sizeof(vis)); int i,j; for(i=0;i<=n;i++) { for(j=0;j<=n;j++) { w[i][j]=INF; if(i==j) w[i][j]=0; } d[i]=INF; }}int DFS(int k,int n){ if(s[k]) return s[k]; if(k == 2) return 1; for(int i=1; i<=n; i++) { if(w[k][i]!=INF && d[k]>d[i]) s[k]+=DFS(i,n); } return s[k];}struct cmp{ bool operator()(int a,int b) { return a>b; }};int main(){ int n,m; while(scanf("%d",&n),n!=0) { init(n); scanf("%d",&m); int i,a,b,d1; for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&d1); if(d1<w[a][b]) w[a][b]=w[b][a]=d1; } d[2]=0; //priority_queue<int,vector<int>,cmp> q; typedef pair<int,int> pii; priority_queue<pii,vector<pii>,greater<pii> > q; q.push(make_pair(d[2],2)); while(!q.empty()) { pii t1=q.top(); q.pop(); int t=t1.second; if(vis[t]) continue; vis[t]=1; for(i=1;i<=n;i++) { if(!vis[i]&&d[i]>d[t]+w[t][i]) { d[i]=d[t]+w[t][i]; q.push(make_pair(d[i],i)); } } } printf("%d\n",DFS(1,n)); } return 0;}
然后是WA的代码:
#include <iostream>#include <cstdio>#include <queue>#include <cstring>#define INF 1000010010using namespace std;int d[1005];int vis[1005];int w[1005][1005];int s[1005];void init(int n){ memset(s,0,sizeof(s)); memset(vis,0,sizeof(vis)); int i,j; for(i=0;i<=n;i++) { for(j=0;j<=n;j++) { w[i][j]=INF; if(i==j) w[i][j]=0; } d[i]=INF; }}int DFS(int k,int n){ if(s[k]) return s[k]; if(k == 2) return 1; for(int i=1; i<=n; i++) { if(w[k][i]!=INF && d[k]>d[i]) s[k]+=DFS(i,n); } return s[k];}struct cmp{ bool operator()(int a,int b) { return d[a]>d[b]; //此处修改 }};int main(){ int n,m; while(scanf("%d",&n),n!=0) { init(n); scanf("%d",&m); int i,a,b,d1; for(i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&d1); if(d1<w[a][b]) w[a][b]=w[b][a]=d1; } d[2]=0; priority_queue<int,vector<int>,cmp> q; //此处修改 //typedef pair<int,int> pii; //priority_queue<pii,vector<pii>,greater<pii> > q; q.push(2); while(!q.empty()) { int t=q.top(); q.pop(); //int t=t1.second; if(vis[t]) continue; vis[t]=1; for(i=1;i<=n;i++) { if(!vis[i]&&d[i]>d[t]+w[t][i]) { d[i]=d[t]+w[t][i]; q.push(i); //此次修改~~ } } } printf("%d\n",DFS(1,n)); } return 0;}
感觉对的~~但是wa了 难道priority queue 用法有误么~~
~~知道问题了~~原来是priority_queue有问题~看看实现就知道了~q.push().q.pop() 都会排序
- hdu1142
- hdu1142
- hdu1142
- hdu1142
- HDU1142
- 哎hdu1142
- hdu1142-记忆化搜索
- hdu1142(最短路+DFS)
- hdu1142 dijstra + 记忆化搜索
- hdu1142(dijastra+记忆化搜索)
- a walk throught the forest(HDU1142)
- hdu1142 A Walk Through the Forest
- HDU1142 A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142-A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142-A Walk Through the Forest-最短路+dfs
- Ubuntu(友帮拓)下安装jdk*.bin需要注意
- win2linux & linux2win
- HIVE 通过 MYSQL 保存元数据
- 双向链表(二级指针实现)
- A Brand New World
- hdu1142
- 算法学习笔记----堆排序
- Extjs3.x版本 选择复制
- 算法入门
- 动态内存分分配
- 设计模式与软考试题之模板方法模式
- delphi 共享变量(Absolute Address)
- Tomcat和Apache服务器访问本地路径的项目配置
- emacs学习(五)