POJ2253 Frogger
题意:
在第一块石头到到第二块石头的通路中,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中有一个最大距离,如果有多条通路,则求出所有通路的最大距离,并把这些最大距离作比较,把最小的一个最大距离输出。
dijkstra和spfa 写的 总感觉还是dijkstra比较好理解 。。。。。。 做了poj1797后 才发现 果然还是spfa最好用
spfa:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxn = 101000, INF = 0xfffffff; int head[maxn],vis[maxn]; double d[maxn]; int n; struct tmp{ double x,y; }Tmp[maxn]; struct node{ int u,v,next; double d; }Node[maxn]; void add(int u,int v,double d,int i) { Node[i].u = u; Node[i].v = v; Node[i].d = d; Node[i].next = head[u]; head[u] = i; } void spfa(int s) { mem(vis,0); queue<int> Q; for(int i=0;i<=n;i++) d[i] = INF; d[s] = 0; Q.push(s); vis[s] = 1; while(!Q.empty()) { int x = Q.front();Q.pop(); vis[x] = 0; for(int i=head[x]; i!=-1; i=Node[i].next) { node e = Node[i]; if(d[e.v] > max(d[x],e.d)) { d[e.v] = max(d[x],e.d); if(!vis[e.v]) { Q.push(e.v); vis[e.v] = 1; } } } } } int main() { int ans = 0; while(cin>>n && n) { int cnt = 0; mem(head,-1); for(int i=1;i<=n;i++) { cin>>Tmp[i].x>>Tmp[i].y; } for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { add(i,j,sqrt((Tmp[i].x-Tmp[j].x)*(Tmp[i].x-Tmp[j].x) + (Tmp[i].y-Tmp[j].y)*(Tmp[i].y-Tmp[j].y)),cnt++); add(j,i,sqrt((Tmp[i].x-Tmp[j].x)*(Tmp[i].x-Tmp[j].x) + (Tmp[i].y-Tmp[j].y)*(Tmp[i].y-Tmp[j].y)),cnt++); } } spfa(1); printf("Scenario #%d\n",++ans); printf("Frog Distance = %.3f\n",d[2]); cout<<endl; } return 0; }
dijkstra:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <cmath> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxn = 10010, INF = 0xfffffff; int n,m; int vis[maxn]; double d[maxn]; struct node2{ double x,y; }Node[maxn]; struct edge{ int u,v; double d; edge(int u,int v,double d) { this->u = u; this->v = v; this->d = d; } }; vector<edge> Edge; vector<int> G[maxn]; struct node{ int u; double d; node(double d,int u) { this->d = d; this->u = u; } bool operator < (const node& a) const { return d > a.d; } }; void add(int u,int v,double d) { Edge.push_back(edge(u,v,d)); G[u].push_back(Edge.size()-1); } void dijkstra(int s) { priority_queue<node> Q; for(int i=0;i<=n;i++) d[i] = INF; d[s] = 0; mem(vis,0); Q.push(node(0,s)); while(!Q.empty()) { node x = Q.top();Q.pop(); int u = x.u; if(vis[u]) continue; vis[u] = 1; for(int i=0;i<G[u].size();i++) { edge e = Edge[G[u][i]]; if(d[e.v] > max(d[u], e.d)) { d[e.v] = max(d[u], e.d); Q.push(node(d[e.v],e.v)); } } } } int main() { int cnt = 0; while(cin>>n && n) { for(int i=0;i<=n;i++) G[i].clear(); Edge.clear(); for(int i=1;i<=n;i++) { cin>>Node[i].x>>Node[i].y; } for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) { add(i,j,sqrt((Node[i].x-Node[j].x)*(Node[i].x-Node[j].x)+(Node[i].y-Node[j].y)*(Node[i].y-Node[j].y))); add(j,i,sqrt((Node[i].x-Node[j].x)*(Node[i].x-Node[j].x)+(Node[i].y-Node[j].y)*(Node[i].y-Node[j].y))); } dijkstra(1); printf("Scenario #%d\n",++cnt); printf("Frog Distance = %.3f\n",d[2]); cout<<endl; } return 0; }
Floyd:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define mem(a,b) memset(a,b,sizeof(b)) using namespace std; const int maxn = 202, INF = 2000; struct edge{ double x,y; }Edge[maxn]; double d[maxn][maxn]; int main() { int n,cnt = 0; while(cin>>n && n) { for(int i=1;i<=n;i++) cin>>Edge[i].x>>Edge[i].y; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) d[i][j] = d[j][i] = sqrt(pow(Edge[i].x-Edge[j].x,2)+pow(Edge[i].y-Edge[j].y,2)); for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) d[i][j] = min(d[i][j],max(d[i][k],d[k][j])); printf("Scenario #%d\n",++cnt); printf("Frog Distance = %.3f\n",d[1][2]); cout<<endl; } return 0; }
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。