#
poj挂了
dijkstra变种
wa了3次..
pe一次
另外orz一下样例...太完美了..对我做题没任何帮助
#include <iostream>
#include <math.h>
using namespace std;
struct node
{
int x,y;
};
const int MAXN=201;
node point[MAXN];
double dist[MAXN];
double answer[MAXN];
bool hash[MAXN];
int n;
inline double distances(int u,int v)
{
double value=(point[u].x-point[v].x)*(point[u].x-point[v].x)+(point[u].y-point[v].y)*(point[u].y-point[v].y);
return sqrt(value);
}
double max(double x,double y)
{
return x>y?x:y;
}
int main()
{
int num=0;
while(1)
{
cin>>n;
if (0==n) break;
for (int i=1;i<=n;i++)
{
cin>>point[i].x>>point[i].y;
}
memset(dist,0x7f,sizeof(dist));
memset(answer,0x7f,sizeof(dist));
memset(hash,0,sizeof(hash));
dist[1]=0.0f;
answer[1]=0.0f;
for (int i=1;i<=n;i++)
dist[i]=distances(1,i);
for (int i=1;i<n;i++)
{
int u=0;
double min=0x7fffffff;
for (int j=1;j<=n;j++)
{
if (hash[j]) continue;
if (min>dist[j]) {min=dist[j];u=j;}
}
hash[u]=true;
if (0==u) break;
for (int j=1;j<=n;j++)
{
{
if (dist[j]>max(dist[u],distances(u,j))) dist[j]=max(dist[u],distances(u,j));
}
}
}
printf("%s%d\n","Scenario #",++num);
printf("%s%.3f\n\n","Frog Distance = ",dist[2]);
}
return 0;
}
WA了多次.因为构图时少打了个=号....
枚举最高rank,多次构图,然后就最短路 O(N^3)
以后要注意静态调试
#include <iostream>
//#include <fstream>
#include <math.h>
using namespace std;
//ifstream fin("t1062.in");
const int MAXN=101;
#define INF 2147483647
int p[MAXN],l[MAXN],x;
int u[MAXN][MAXN];
int q[MAXN];
int m,n;
void dijkstra()
{
int dist[MAXN];
bool hash[MAXN];
int answer=INF;
for (int i=n;i>=1;--i)
{
int maxRank=l[i];
for (int j=1;j<=n;++j)
if (l[j]>maxRank||maxRank-l[j]>m) q[j]=1;
else q[j]=0;
memset(hash,0,sizeof(hash));
for (int j=1;j<=n;++j)
dist[j]=p[j];
for (int j=1;j<=n;++j)
{
int u1=0;
int min=INF;
for (int k=1;k<=n;++k)
{
if (q[k]) continue;
if (hash[k]) continue;
if (min>dist[k]) {min=dist[k];u1=k;}
}
hash[u1]=true;
if (u1==0) break;
if (dist[u1]==0x7f) break;
for (int k=1;k<=n;++k)
{
if (q[k]) continue;
if (u[u1][k]==0x7f) continue;
if (dist[k]>dist[u1]+u[u1][k])
{
dist[k]=dist[u1]+u[u1][k];
}
}
}
if (answer>dist[1]) answer=dist[1];
}
cout<<answer<<endl;
}
int main()
{
memset(u,0x7f,sizeof(u));
cin>>m>>n;
for (int i=1;i<=n;i++)
{
cin>>p[i]>>l[i]>>x;
for (int j=1;j<=x;j++)
{
int t,v;
cin>>t>>v;
u[t][i]=v;
}
}
dijkstra();
return 0;
}
还是bellman-ford
#include <iostream>
#include <vector>
//#include <fstream>
using namespace std;
//ifstream fin("t3259.in");
struct node
{
int u,v,w;
};
vector<node> edge_vec;
int f;
int n,m,w;
bool bellman()
{
int dist[10000];
memset(dist,0x7f,sizeof(dist));
dist[edge_vec.at(0).u]=0;
int flag;
for (int i=1;i<=n;i++)
{
flag=0;
for (int j=0;j<edge_vec.size();j++)
{
node edge=edge_vec.at(j);
if (dist[edge.v]>dist[edge.u]+edge.w)
{dist[edge.v]=dist[edge.u]+edge.w;flag=1;}
}
if (!flag) return false;
}
for (int i=0;i<edge_vec.size();i++)
{
node edge=edge_vec.at(i);
if (dist[edge.v]<dist[edge.u]+edge.w)
return true;
}
return false;
}
int main()
{
cin>>f;
while (f--)
{
cin>>n>>m>>w;
edge_vec.clear();
for (int i=1;i<=m;i++)
{
int u_,v_,w_;
cin>>u_>>v_>>w_;
node edge;
edge.u=u_;
edge.v=v_;
edge.w=w_;
edge_vec.push_back(edge);
edge.u=v_;
edge.v=u_;
edge.w=w_;
edge_vec.push_back(edge);
}
for (int i=1;i<=w;i++)
{
int u_,v_,w_;
cin>>u_>>v_>>w_;
node edge;
edge.u=u_;
edge.v=v_;
edge.w=-w_;
edge_vec.push_back(edge);
}
if (bellman()) cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
Bellman-Ford
看题看了半天..
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
//ifstream fin("t1860.in");
struct node
{
int u,v;
double r,c;
};
vector<node> edge_vec;
int n,m,s;
int f;
#define eps 1e-8
double v;
double dist[200];
void relax(int x,int y,double r,double c)
{
if (dist[y]+eps<(dist[x]-c)*r)
{f=1;dist[y]=(dist[x]-c)*r;}
}
bool bellman()
{
for (int i=1;i<=n;i++)
dist[i]=0;
dist[s]=v;
while(dist[s]<=v+eps)
{
f=0;
for (int j=0;j<edge_vec.size();j++)
{
node edge=edge_vec.at(j);
relax(edge.u,edge.v,edge.r,edge.c);
}
if (!f) break;
}
if (dist[s]>v+eps) return true;
else return false;
}
int main()
{
cin>>n>>m>>s>>v;
for (int i=1;i<=m;i++)
{
int x,y;
double rxy,cxy,ryx,cyx;
cin>>x>>y>>rxy>>cxy>>ryx>>cyx;
node edge;
edge.u=x;
edge.v=y;
edge.r=rxy;
edge.c=cxy;
edge_vec.push_back(edge);
edge.u=y;
edge.v=x;
edge.r=ryx;
edge.c=cyx;
edge_vec.push_back(edge);
}
if (bellman()) cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
// system("pause");
return 0;
}