1087. All Roads Lead to Rome (30)

来源:互联网 发布:2016网络十大洗脑神曲 编辑:程序博客网 时间:2024/05/21 13:22

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format “City1 City2 Cost”. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness – it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format “City1->City2->…->ROM”.

Sample Input:
6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1
Sample Output:
3 3 195 97
HZH->PRS->ROM
DFS算法:

#include <iostream>#include<map>#include<cstdio>#include<limits>#include<vector>#define NN 200#define INT_MAX 1000000#define INT_MIN -1000000using namespace std;string name[NN];int hap[NN];int road[NN][NN];int curCost=0,curHap=0,curNum=0;int minCost=INT_MAX,maxHap=INT_MIN;int minNum=INT_MAX;int roadNum=0;map<string,int> city;vector<int> minRoad;vector<int> curRoad;bool isReached[NN];void DFS(int cur){    if(curCost>minCost)        return;    if(cur==city["ROM"])    {        bool better=false;        bool tem=false;        if(curCost<minCost)        {            better=true;            tem=true;        }        else if(curCost==minCost)        {            roadNum++;            if(curHap>maxHap)            {                better=true;            }            else if (curHap==maxHap)            {                if(curNum<minNum)                    better=true;            }        }        if(better)        {            if(tem)            {                roadNum=1;            }            minCost=curCost;            maxHap=curHap;            minNum=curNum;            minRoad = curRoad;        }    }    for(int i=0;i<NN;i++)//遍历每个节点    {        if(!isReached[i]&&road[cur][i]>0)        {            isReached[i]=true;            curCost+=road[cur][i];            curHap+=hap[i];            curNum++;            curRoad.push_back(i);            DFS(i);            curRoad.pop_back();            ///恢复            curNum--;            curHap-=hap[i];            curCost-=road[cur][i];            isReached[i] = false;        }    }}int main(){    int N,K;// 2<=N<=200    string start;    cin>>N>>K>>start;    int temp=N;    city[name[0]=start]=0;    for (int i=1;i<N;i++)    {        cin>>name[i]>>hap[i];        city[name[i]]=i;    }    temp=K;    while(temp--)///包括0    {        string sname;        cin>>sname;        int x=city[sname];        cin>>sname;        int y=city[sname];        int z;        cin>>z;        road[x][y]=road[y][x]=z;    }    DFS(0);    cout<<roadNum<<" ";    cout<<minCost<<" ";    cout<<maxHap<<" ";    cout<<maxHap/(minNum)<<endl;    cout<<start;    for(int i=0;i<minRoad.size();i++)        cout<<"->"<<name[minRoad[i]];    return 0;}/*6 7 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1*/

结题思路:先求cost最小的路径,如果相等,记录相等的次数,在cost每一次更新时都重新赋值为1,如果cost相等,判断hap,依次类推,判断经过的城市数。然后递归搜索每个可达的路径,在回溯时注意要将值恢复,访问标记抹掉。
问题:此题在pat官网上可以通过,在牛客网上有几个案例超时。
Dijkstra 算法:
该算法有四组案例未通过

#include<iostream>#include<cstdio>#define MAX 201#include<map>#include<vector>#include<stdio.h>#include<stack>#include<limits.h>using namespace std;typedef struct{int verNum;int arcNum;int arcs[MAX][MAX];}MGraph;map<string,int>city;int hap[MAX];///每个城市的快乐点MGraph G;int numRoads=1;///最短路径的数量string name[MAX];///城市名字和编号的映射int totalHap[MAX];///到某个城市的最大快乐点int citis[MAX];///到某个城市经过的城市数量void ShortestPath_DIJ(MGraph G,int v0, int p[],int D[])//p 存路径,倒叙,D 花费最少{    int fin[MAX];    int v;    vector<int> teMinRoad;    for(int w=0;w<G.verNum;w++)            p[w]=-1;//设空路径    for( v=0;v<G.verNum;v++)    {        fin[v]=false;//访问标记        D[v]=G.arcs[v0][v];//给D赋初值 本题赋为INT_MAX        if(D[v]<INT_MAX)        {            p[v]=v0;//v0可直接到达的点            totalHap[v]=hap[v];            citis[v]=1;        }    }    D[v0]=0;fin[v0]=true;    for(int i=1;i<G.verNum;i++)      {           int min=INT_MAX;           //找到没有被访问过,且V0到该点距离最小的点           int change =false;           for(int w=0;w<G.verNum;w++)            if(!fin[w])                if(D[w]<min)                {                    v=w;                    min=D[w];                }            fin[v]=true;//将该点设为访问过            //将该点加入S中,只会影响其以后的邻接点。所以要更新其以后邻接点的D[w]            for(int w=0;w<G.verNum;w++)                if(G.arcs[v][w]<INT_MAX)               {                    if(!fin[w]&&(min+G.arcs[v][w]<D[w]))//设为最大值 结果溢出了                    {                        D[w]=min+G.arcs[v][w];                        totalHap[w]=totalHap[v]+hap[w];                        citis[w]=citis[v]+1;                        p[w]=v;///这就是路径                        if(w==city["ROM"])                            change=true;                    }                    else if(min+G.arcs[v][w]==D[w])                    {                       if(w==city["ROM"])                        {                            if(change)                            {                                numRoads=1;                                change=false;                            }                            numRoads++;                        }                        if(totalHap[w]<totalHap[v]+hap[w]||totalHap[w]==totalHap[v]\                            &&citis[w]>citis[v]+1)                        {                            p[w]=v;                            totalHap[w]=totalHap[v]+hap[w];                            citis[w]=citis[v]+1;                        }                    }               }      }}int main(){   int N=0,K=0;// 2<=N<=200   // string start;   char  start[4];   int temp;   scanf("%d%d%s",&N,&K,start);   int p[MAX],D[MAX];///p 路径 D 最短距离    for(int i=0;i<MAX;i++)    for(int j=0;j<MAX;j++)    {           G.arcs[i][j]=INT_MAX;           D[i]=INT_MAX;    }    G.verNum=N;    G.arcNum=K;    city[name[0]=start]=0;    for (int i=1;i<N;i++)    {// cin>>name[i]>>hap[i];        char t[4];        scanf("%s%d",t,&hap[i]);        name[i]=t;        city[name[i]]=i;///自己设置一个整数编号    }    temp=K;    while(temp--)    {        string sname;        cin>>sname;        int x=city[sname];        cin>>sname;        int y=city[sname];        int z;        cin>>z;        G.arcs[x][y]=G.arcs[y][x]=z;    }    ShortestPath_DIJ(G,0,p,D);    int rom=city["ROM"];    cout<<numRoads<<" "<<D[rom]<<" "<<totalHap[rom]<<" "<<totalHap[rom]/citis[rom]<<endl;    stack<int> sk;    for(int i=rom;i!=0;i=p[i])    {       sk.push(i);    }    cout<<start;    while(!sk.empty())    {        int i=sk.top();        sk.pop();        cout<<"->"<<name[i];    }    return 0;}
0 0
原创粉丝点击