Roads ro Rom


#define MAPIMPLEMENT_H_INCLUDED
#include<iostream>
#include<new>
#include<string>
#include<map>
#define MaxVertexNum 100
#define INFINITY 65535
using namespace std;
typedef  struct  node{
    int AdjV;
    struct  node  *Next;
    int weight;
} EdgeNode;
typedef  int  VertexType;
typedef  struct  Vnode{
    VertexType  Vertex;
    EdgeNode  *FirstEdge;
} VertexNode;
typedef  struct{
    VertexNode  adjlist[MaxVertexNum];
    int  n, e;
} ALGraph;
void CreateALGraph( ALGraph *G )
{
    map<string,int> Map;
    Map["HZH"]=0;
    Map["PKN"]=2;
    Map["GDN"]=3;
    Map["PRS"]=4;
    Map["ROM"]=1;
    Map["BLN"]=5;
    int i, j, k, w;
    EdgeNode *edge;
    cin>>G->n>>G->e;
    string word;
    string word2;
    cin>>word;
    G->adjlist[Map[word]].Vertex=0;
    G->adjlist[Map[word]].FirstEdge=NULL;
    for ( i=1; i < G->n; i++ ) {
        cin>>word;
        cin>>G->adjlist[Map[word]].Vertex;
       G->adjlist[Map[word]].FirstEdge = NULL;
    }
    for ( k=0; k < G->e; k++ ){
       cin>>word>>word2>>w;
       i=Map[word];
       j=Map[word2];
       edge = new EdgeNode;
       edge->AdjV = j;
       edge->weight = w;
       edge->Next = G->adjlist[i].FirstEdge;
       G->adjlist[i].FirstEdge = edge;
       edge = new EdgeNode;
       edge->AdjV = i;
       edge->Next = G->adjlist[j].FirstEdge;
       edge->weight = w;
       G->adjlist[j].FirstEdge = edge;
    }
}
#endif // MAPIMPLEMENT_H_INCLUDED
 
 
#include "Dijkstra.h"
#include <iostream>
#include <vector>

using namespace std;
int F(ALGraph* G,int i,int j);
int PATH(vector<int> &path,ALGraph *G);
int HAPPY(ALGraph *G,vector<int> &path);
void DFS(ALGraph *G,bool visited[],vector<int> &path,vector<int> &res,int i,int j,int value,int *h,int *r);

int main()
{
    map<int,string> Map;
    Map[0]="HZH";
    Map[2]="PKN";
    Map[3]="GDN";
    Map[4]="PRS";
    Map[1]="ROM";
    Map[5]="BLN";
    ALGraph M;
    CreateALGraph(&M);
    int i,j;
    i=0;
    j=1;//起点和终点固定
    int p=Dijkstra(i,j,&M);
    bool visited[M.n]={0};
    vector<int> v1;
    vector<int> v2;
    int happy=0;
    int route=0;
    DFS(&M,visited,v1,v2,i,j,p,&happy,&route);
    cout<<route<<" ";
    cout<<p<<" ";
    cout<<happy<<" ";
    int average=happy/(v2.size()-1);
    cout<<average<<" "<<endl;
    for(int i=0;i<v2.size();i++)
    {
        if(i==v2.size()-1)
            cout<<Map[v2[i]];
        else
            cout<<Map[v2[i]]<<"->";
    }
    return 0;
}
void DFS(ALGraph *G,bool visited[],vector<int> &path,vector<int> &res,int i,int j,int value,int *h,int *r)
{
    visited[i]=true;
    path.push_back(i);
    if(i==j)
    {
        int sum=PATH(path,G);
        int sum2=0;
        if(sum==value)//如果是最短路径之一
        {
            ++(*r);
            sum2=HAPPY(G,path);
            if(sum2>*h)
            {
                *h=sum2;
                res=path;
            }
            if(sum2==*h&&(res.size()>path.size()))
            {
                res=path;
            }
        }
        return;
    }
    for(EdgeNode *w=G->adjlist[i].FirstEdge;w!=NULL;w=w->Next)
    {
        int k=w->AdjV;
        if(visited[k]==false)
        {
            DFS(G,visited,path,res,k,j,value,h,r);
            //改会辅助的全局变量
            path.pop_back();
            visited[k]=false;
        }
    }
}

int F(ALGraph* G,int i,int j)
{
    int k;
    int p;
    for(EdgeNode *w=G->adjlist[i].FirstEdge;w!=NULL;w=w->Next)
    {
        k=w->AdjV;
        if(k==j)
        {
            p=w->weight;
            break;
        }
    }
    return p;
}
int PATH(vector<int> &path,ALGraph *G)
{
    int sum=0;
    for(int i=0;i<path.size();i++)
    {
        if(i!=path.size()-1)
            sum+=F(G,path[i],path[i+1]);
    }
    return sum;
}

int HAPPY(ALGraph *G,vector<int> &path)
{
    int sum=0;
    for(int i=1;i<path.size();i++)
        sum+=G->adjlist[path[i]].Vertex;
    return sum;
}


 
 
 
 
 

猜你喜欢

转载自blog.csdn.net/qq_37566910/article/details/78330508
ROM