poj 1122 FDNY to the Rescue! 最短路径 dijkstra算法
来源:互联网 发布:万网域名icp备案 编辑:程序博客网 时间:2024/06/06 13:01
这道题做了好多发,用sscanf()读入提交老是RE,不知道为甚么,谁能帮我解决下,后来改用isstringstream就ac了。
sscanf() RE,大牛帮我看一下
gets(s); gets(s); int k=0; i=0; int len=strlen(s); while(s[i]==' ') i++; while(i<len) { sscanf(s+i,"%d",&b[k]); while(i<len&&s[i]<='9'&&s[i]>='0') i++; while(i<len&&s[i]==' ') i++; k++; }
本题注意点
1: 逆向存图
2:一个点到另一个点值可能是0
3:存储路径。但是我的只能记录一条最短路径。但是能过。
之后ac代码
#include<iostream>#include<sstream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;const int inf = 0x3f3f3f3f;int n;int a[25][25];int b[25];int c[25];int dist[25];int path[25];int vis[25];bool cmp(int a,int b){ return dist[a]<dist[b];}void dijkstra(int u){ int i,j; memset(vis,0,sizeof(vis)); memset(path,-1,sizeof(path)); vis[u]=1; for(i=1;i<=n;i++) { dist[i]=a[u][i]; if(dist[i]<inf) path[i]=u; } path[u]=-1; int minn; int v; int k; for(k=1;k<n;k++) { minn=inf; v=u; for(i=1;i<=n;i++) { if(vis[i]==0&&dist[i]<minn) { minn=dist[i]; v=i; } } vis[v]=1; if(v!=u) { for(i=1;i<=n;i++) { if(vis[i]==0&&dist[v]+a[v][i]<dist[i]) { dist[i]=dist[v]+a[v][i]; path[i]=v; } } } } return ;}int main(){ char s[256]; int i,j; scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { a[i][j]=inf; } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); if(a[i][j]==-1) a[i][j]=inf; } } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) swap(a[i][j],a[j][i]); } gets(s); string str; getline(cin,str); istringstream is(str); int k=0; while(is>>b[k]) k++; dijkstra(b[0]); for(i=1;i<=n;i++) c[i]=i; sort(c+1,c+n+1,cmp); cout<<"Org\tDest\tTime\tPath"<<endl; for(i=1;i<=n;i++) { for(j=1;j<k;j++) { if(c[i]==b[j]) { cout<<c[i]<<"\t"<<b[0]<<"\t"<<dist[c[i]]<<"\t"; int tt=path[c[i]]; cout<<c[i]; while(tt!=-1) { cout<<"\t"<<tt; tt=path[tt]; } cout<<endl; break; } } } return 0;}
0 0
- poj 1122 FDNY to the Rescue! 最短路径 dijkstra算法
- POJ 1122 FDNY to the Rescue!(最短路径)
- poj 1122 FDNY to the Rescue! (dijkstra)
- POJ 1122 FDNY to the Rescue! 反向dijkstra
- ZPJ - 1053 FDNY to the Rescue!(最短路 Dijkstra),POJ 1122
- poj 1122 FDNY to the Rescue! (最短路+打印路径)
- poj 1122||zoj 1053 FDNY to the Rescue!(最短路)
- poj 1122 FDNY to the Rescue! 最短路
- POJ--1122--FDNY to the Rescue!【最短路】
- poj1122&zoj1053 FDNY to the Rescue!(dijkstra+输出路径)
- Poj 1122 FDNY to the Rescue
- POJ:1122 FDNY to the Rescue!
- poj 1122 FDNY to the Rescue!
- POJ 1122 FDNY to the Rescue!
- poj 1122 FDNY to the Rescue!
- POJ 1122 FDNY to the Rescue!
- POJ-1122-FDNY to the Rescue!(迪杰斯特拉 反向建图 输出路径)
- zoj 1053 && poj 1122 FDNY to the Rescue!
- Xcode下没有代码提示的解决方法
- ArcGIS教程:创建特征
- 学而时习之,温故而知新——近期新接触的概念、重温以前的概念
- 【Struts2框架】第四节Struts标签-通用标签简述与常用标签大例子
- poj 2184 Cow Exhibition 负值01背包
- poj 1122 FDNY to the Rescue! 最短路径 dijkstra算法
- C/C++类型强制转换
- haproxy+keepalived实现高可用负载均衡
- Java Applet 讲解
- 【CODEFORCES】 A. Initial Bet
- 用Gson解析复杂的Json字符串
- poj 1789 Truck History
- 琐碎问题
- linux下oracle手动启动服务和监听命令