#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#define MAX_NUM 10000
using namespace std;
int vex_num=130;
int edge_num=175;
double dis[130];
struct VEX
{
struct EDGE *p;
string id;
int x,y;
bool flag;
struct VEX *father;
struct EDGE *faedge;
double length;
};
VEX vexs[130];
VEX launch[60];
struct EDGE {
string iv,jv; //顶点名
int ivex, jvex;//顶点在顶点数组的位置
struct EDGE *ilink , *jlink;
int road ; //路况,主路1,次路0
double len();
};
double EDGE::len()
{
double sum=(vexs[ivex].x-vexs[jvex].x)*(vexs[ivex].x-vexs[jvex].x)+(vexs[ivex].y-vexs[jvex].y)*(vexs[ivex].y-vexs[jvex].y);
sum=sqrt(sum);
if (road==1)
sum=0.6*sum;
return sum;
}
struct EDGE edge[200];
bool SPFA()//zui
{
int source=109;
queue<int> q;
q.push(source); //超级源节点入队
int vex=-1;
bool mark[130];
for(int i=0;i<vex_num;i++)//从0开始
{
dis[i]=MAX_NUM;
vexs[i].father=NULL;//父点
vexs[i].faedge=NULL; //父边
mark[i]=false;
}
dis[source]=0; //出发点的距离设置为0
while (!q.empty() )
{
vex=q.front(); q.pop(); //访问对首元素 // 清除第一个元素
mark[vex]=false; //出队,做记录
struct EDGE *ip; //边指针
ip=vexs[vex].p; // 顶点指向的第一条边,ip是边ip
while(ip!=NULL)
{
int next=(ip->ivex != vex ? ip->ivex:ip->jvex);//顶点 vex 指向这个边ip , 这个边ip 有2个值,一个vex ,另一个next .
int orient=(ip->ivex == vex ?1:0);//规定:第一个位置是vex 为正向,值1
//cout<<"next: "<< next<< endl;
double tprice=ip->len();
if ( dis[next]>dis[vex]+tprice)
{
dis[next]=dis[vex]+tprice;
//cout<<vex<<" vexdis:"<<dis[vex]<<" tprice:"<<tprice<<" "<<next <<" ndis:"<<dis[next]<<endl;
vexs[next].father=&vexs[vex];//父点
vexs[next].faedge=ip; //父边
if( (mark[next]!=true) ) //不在队
{
q.push(next);
mark[next]=true;
}
}
ip=(ip->ivex == vex?ip->ilink:ip=ip->jlink); ///从下一条边,找下一个邻接点
}
}
return (dis[vex_num-1]==MAX_NUM?false:true);
}
void vex_sort() //按邻边数,排序顶点
{
struct VEX temp;
int i,j;
for ( j = 0; j < 60 - 1; j++)
for ( i = 0; i < 60 - 1 - j; i++)
{
if(launch[i].length > launch[i + 1].length)
{
temp = launch[i];
launch[i] = launch[i + 1];
launch[i + 1] = temp;
}
}
// for ( int i = 0; i < vex_num/2; i += 1)
// {
// cout<<"servex "<<servex[i].vex<<" : "<<servex[i].num<<endl;
// }
}
int main()
{
double v[3][2]={
{70,45},
{60,35},
{50,30}};
ifstream invex;
invex.open ("vex.txt", ifstream::in);
ifstream inedge;
inedge.open("edge.txt",ifstream::in);
ofstream route;
route.open("route.txt",ofstream :: out);
for(int i=0; i<130; i++)
{
invex>>vexs[i].id>>vexs[i].x>>vexs[i].y;
}
for(int i=0; i<175; i++)
{
inedge>>edge[i].iv>>edge[i].jv>>edge[i].road;
}
for (int k=0; k<edge_num; k++)
{
edge[k].ilink=edge[k].jlink=NULL;
for (int i=0; i<vex_num; i++)
{
if (edge[k].iv.compare(vexs[i].id)==0)
edge[k].ivex=i;
if (edge[k].jv.compare(vexs[i].id)==0)
edge[k].jvex=i;
}
}
for (int id= 0; id< vex_num; id += 1) //节点编号 vexs[id] 数组的索引
{
int tem=-1;
for (int j = 0; j < edge_num; j += 1) // j 是数组edge[j] 的索引
{
if (edge[j].iv.compare(vexs[id].id)==0 ||edge[j].jv.compare(vexs[id].id)==0 )
{
if (tem!=-1)
{
if (edge[tem].iv.compare(vexs[id].id)==0)
{
edge[tem].ilink = &edge[j];
// cout<<"i--link: "<<"("<<edge[tem].ivex<<", "<<edge[tem].jvex<<")"<<"("<<edge[tem].ilink->ivex<<","<<edge[tem].ilink->jvex<<")"<<endl;
}
else
{
edge[tem].jlink = &edge[j];
// cout<<"j--link: "<<"("<<edge[tem].ivex<<", "<<edge[tem].jvex<<")"<<"("<<edge[tem].jlink->ivex<<","<<edge[tem].jlink->jvex<<")"<<endl;
}
}
else
{
vexs[id].p=&edge[j];
}
tem=j;
}
}
}
SPFA();
//for(int i=0; i<130; i++)
//{
// cout<<vexs[i].id<<" : " <<dis[i]<<endl;
//}
for (int i=0; i<60; i++)
{
launch[i]=vexs[i+8];
launch[i].length=dis[i+8];
}
vex_sort();
//for(int i=0; i<60; i++)
//{
// cout<<launch[i].id<<" 到待机点的距离: " <<launch[i].length<<",路过的点:"<<endl;
// route<<launch[i].id<<" 到待机点的距离: " <<launch[i].length<<",路过的点:"<<endl;
// VEX *tp=launch[i].father;
// while (tp!=NULL)
// {
// cout<< tp->id<<", ";
// route<< tp->id<<", ";
// tp=tp->father;
// }
// cout<<endl;
// route<<endl;
//}
for(int i=0; i<60; i++)
{
cout<<launch[i].id<<" 到待机点的距离: " <<launch[i].length<<",路过的点:"<<endl;
route<<launch[i].id<<" 到待机点的距离: " <<launch[i].length<<",路过的点:"<<endl;
VEX *tp=&launch[i];
double times=0;
while (tp!=NULL)
{
cout<< tp->id<<", ";
route<< tp->id<<", ";
if (tp->faedge!=NULL)
{
if (i<6)
{
if (tp->faedge->road==0)
times+=tp->faedge->len()/v[2][1];
else
times+=tp->faedge->len()/v[2][0];
}
if (i>5&&i<9)
{
if (tp->faedge->road==0)
times+=tp->faedge->len()/v[1][1];
else
times+=tp->faedge->len()/v[1][0];
}
if (i>8)
{
if (tp->faedge->road==0)
times+=tp->faedge->len()/v[0][1];
else
times+=tp->faedge->len()/v[0][0];
}
}
tp=tp->father;
}
cout<<" time:"<<times<<endl;
route<<" time:"<<times<<endl;
}
//ofstream fedge;
//fedge.open("fedge.txt",ofstream :: out);
//cout<<"边信息:"<<endl;
//for(int i=0; i<175; i++)
//{
// cout<<edge[i].iv<<" " <<edge[i].jv<<" "<<"位置:";
// fedge<<edge[i].iv<<" " <<edge[i].jv<<" "<<"位置:";
// cout<<edge[i].ivex<<" "<<edge[i].jvex<<" "<<edge[i].road;
// fedge<<edge[i].ivex<<" "<<edge[i].jvex<<" "<<edge[i].road;
// cout<<" 地址:"<<edge[i].ilink<<", "<<edge[i].jlink<<". len: "<<edge[i].len()<<endl;
// fedge<<". len: "<<edge[i].len()<<endl;
//}
//
invex.close();
inedge.close();
route.close();
system("pause");
}
导弹选路问题
最新推荐文章于 2023-12-18 18:01:45 发布