Ideal Path UVA
来源:互联网 发布:青岛网站排名seo 编辑:程序博客网 时间:2024/04/29 11:34
仿照刘汝佳的代码用vector却超时。。。
只能自己再写一个用队列实现的。。
而且还要注意这个地方要vis数组。。(按理说应该知道走过的路自然不会再走啊。。 不懂。。。。)
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<string>#include<cstring>#include<iomanip>#include<iostream>#include<stack>#include<cmath>#include<map>#include<vector>#define ll long long#define inf 0x3f3f3f3f#define INF 1000000000#define bug1 cout<<"bug1"<<endl;#define bug2 cout<<"bug2"<<endl;#define bug3 cout<<"bug3"<<endl;using namespace std;int n,m;int tol;const int maxn=100000 + 5;struct Edge{ int v,nxt,w;}edge[maxn*4];int head[maxn];int vis[maxn];int d[maxn];void addedge(int u,int v,int w){ edge[tol].v=v; edge[tol].w=w; edge[tol].nxt=head[u]; head[u]=tol++;}void rev_bfs(){ memset(vis,0,sizeof(vis)); memset(d,0,sizeof(d)); queue<int>que; que.push(n); vis[n]=1; d[n]=0; while(!que.empty()){ int u=que.front();que.pop(); for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].v; if(vis[v])continue; vis[v]=1; d[v]=d[u]+1; que.push(v); } }}vector<int>ans;void bfs(){ memset(vis,0,sizeof(vis)); ans.clear(); queue<int >que; queue<int >que2; queue<int>tmp; queue<int>tmp1; que.push(1); vis[1]=1; while(!que.empty()){ int min_color=INF; while(!que.empty()){ int t=que.front();que.pop(); tmp.push(t); tmp1.push(t);//一分为二,第一个是用来找min_color的,第二个就是用来走下一个节点的 } while(!tmp.empty()){ int u=tmp.front();tmp.pop(); if(u==n)break; for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].v; if(d[v]==d[u]-1){ min_color=min(min_color,edge[i].w); } } } ans.push_back(min_color); while(!tmp1.empty()){ int u=tmp1.front();tmp1.pop(); for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].v; if(d[v]==d[u]-1&&!vis[v]&&edge[i].w==min_color){ if(v==n)return; vis[v]=1; que2.push(v); } } } if(que.empty()){ while(!que2.empty()){ int t=que2.front();que2.pop(); que.push(t); } } }}int main(){ while(scanf("%d%d", &n, &m) == 2){ tol=0; memset(head,-1,sizeof(head)); for(int i=1;i<=m;++i){ int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } rev_bfs(); bfs(); printf("%d\n", ans.size()); printf("%d", ans[0]); for(int i = 1; i < ans.size(); i++) printf(" %d", ans[i]); printf("\n"); }}
0 0
- uva 1599 - Ideal Path
- UVa 1599 Ideal Path
- Uva - 1599 - Ideal Path
- UVa 1599 Ideal Path
- UVa 1599 Ideal Path
- UVA 1599 Ideal Path
- Ideal Path UVA
- Ideal Path UVA
- Ideal Path UVA
- uva UVA - 1599 Ideal Path
- UVA 1599(p173)----Ideal Path
- [图论紫书/Ch5] Ideal Path UVA
- UVa 1599 - Ideal Path <两次BFS>
- UVa 1599 - Ideal Path(BFS)
- 理想路径(Ideal Path,UVa 1599)
- UVa 1599 Ideal Path[待AC]
- UVA 1599 Ideal Path (理想路径)
- ACM篇:UVa 1599 -- Ideal Path
- SVN update的时候提示 ……is already loacked
- scipy数值优化与参数估计
- 连接接口查询电话归属地信息
- Seekbar改变EditText的值死循环解决
- 《全面征服》精品源代码、亲测可运行
- Ideal Path UVA
- GraphMat论文总结
- linux下用phpize给PHP动态添加扩展
- 弯曲跑道Shader
- Linked List Cycle II
- 正则小结
- 排序算法Python实现
- vr开发
- 计网|实验一 Cisco packet tracer模拟组建以太网