1087. All Roads Lead to Rome (30) -- Dijkstra经典

来源:互联网 发布:无缝拼图软件 编辑:程序博客网 时间:2024/05/05 19:34

题目地址:http://www.patest.cn/contests/pat-a-practise/1087

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

  • 下面是我一次ac的代码,读题加上敲代码差不多花了50分钟,不过这题仍然是Dijkstra,要仔细读题,条件比较的多,输入输出也需要特别注意
  • 用到了map 来映射城市与编号之间的关系,做题的时候也需要大致画个图,来观察一下结果。这题算是Dijkstra 条件最多的了,类似的题目很多,基本上多加练习和总结,应该可以吃透
#include <stdio.h>#include <iostream>  #include <stdlib.h>#include <string>#include <string.h>#include <algorithm>#include <math.h>#include <map>#include <unordered_map>#include <queue>#include <vector>#include <set>#include <stack>#include <iterator>using namespace std;#define N 202#define INF 9999999int n;int kk;map<string, int> um;// city2nomap<int, string> um2;// no2cityint happiness[N];int mp[N][N];int pathCount[N];int dis[N];int happyAmount[N];int happyAve[N];int pre[N];bool vis[N];int nums[N];void dijkstra(int sta, int en){  int i, j;  for (i = 1; i <= n; i++)  {    vis[i] = false;    dis[i] = INF;    pathCount[i] = 1;    happyAmount[i] = happiness[i];    nums[i] = n;    happyAve[i] = happyAmount[i] / nums[i];    pre[i] = -1;  }  dis[sta] = 0;  vis[sta] = true;  nums[sta] = 1;  int newP = sta;  while (newP != en)  {    for (i = 1; i <= n; i++)    {      if (!vis[i])      {        if (dis[newP] + mp[newP][i] < dis[i] && dis[newP] + mp[newP][i]<INF)        {          dis[i] = dis[newP] + mp[newP][i];          pathCount[i] = pathCount[newP];          happyAmount[i] = happyAmount[newP] + happiness[i];          nums[i] = nums[newP] + 1;          happyAve[i] = happyAmount[i] / (nums[i] - 1);          pre[i] = newP;        }        else if (dis[newP] + mp[newP][i] == dis[i] && dis[newP] + mp[newP][i]<INF)        {          pathCount[i] += pathCount[newP];          if (happyAmount[i] < happyAmount[newP] + happiness[i])          {            happyAmount[i] = happyAmount[newP] + happiness[i];            nums[i] = nums[newP] + 1;            happyAve[i] = happyAmount[i] / (nums[i] - 1);            pre[i] = newP;          }          else if (happyAmount[i] == happyAmount[newP] + happiness[i])          {            int tmpNums = nums[newP] + 1;            if (happyAve[i] < happyAmount[i] / tmpNums)            {              happyAve[i] = happyAmount[i] / tmpNums;              pre[i] = newP;            }          }        }      }    }// for    int minn = INF;    for (i = 1; i <= n; i++)    {      if (!vis[i] && dis[i] < minn)      {        minn = dis[i];        newP = i;      }    }    vis[newP] = true;  }// while}int main(){  //freopen("in", "r", stdin);  int i, j;  string s;  scanf("%d %d", &n, &kk);  cin >> s;  um[s] = 1;  um2[1] = s;  happiness[1] = 0;  for (i = 0; i < n-1; i++)  {    cin >> s;    um[s] = i + 2;    um2[i + 2] = s;    scanf("%d", &happiness[i + 2]);  }  for (i = 1; i <= n; i ++ )  {    for (j = 1; j <= n; j++)    {      mp[i][j] = INF;      mp[j][i] = INF;    }    mp[i][i] = 0;  }  string s1, s2;  int distmp;  for (i = 0; i < kk; i++)  {    cin >> s1 >> s2 >> distmp;    int u = um[s1];    int v = um[s2];    mp[u][v] = distmp;    mp[v][u] = distmp;  }  s = "ROM";  int en = um[s]; // sta = 1;  dijkstra(1, en);  printf("%d %d %d %d\n", pathCount[en], dis[en], happyAmount[en], happyAve[en]);  vector<string> v;  int tmp = en;  int len2 = 0;  while (tmp != -1)  {    len2++;    v.push_back(um2[tmp]);    tmp = pre[tmp];  }  cout << v[len2-1];  for (i = len2-2; i >=0 ; i--)  {    cout << "->" << v[i];  }  printf("\n");  return 0;}

第二次ac代码(2017-2-12)

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> #include <iterator> using namespace std;#define INF 0x7ffffffftypedef long long int LL;const int N = 202;int n, K;string dest;int d;map<string,int> mps2i;map<int,string> mpi2s;int happiness[N];int mp[N][N];bool vis[N];int pre[N];int dist[N];int happyCnt[N];int cityCnt[N];int pathCnt[N];void Dijkstra(){    for(int i=0;i<n;i++)    {        vis[i] = false;        pre[i] = -1;        dist[i] = INF;        happyCnt[i] = -1;        cityCnt[i] = 1;        pathCnt[i] = 1;    }    int newP = mps2i["ROM"];    vis[newP] = true;    dist[newP] = 0;    happyCnt[newP] = happiness[newP];    while(newP != d)    {        for(int i=0;i<n;i++)        {            if(!vis[i] && mp[newP][i] != -1)            {                if(dist[newP] + mp[newP][i] < dist[i])                {                    dist[i] = dist[newP] + mp[newP][i];                    happyCnt[i] = happyCnt[newP] + happiness[i];                    pre[i] = newP;                    cityCnt[i] = cityCnt[newP] + 1;                    pathCnt[i] = pathCnt[newP];                }else if(dist[newP] + mp[newP][i] == dist[i])                {                    pathCnt[i] += pathCnt[newP];                    if(happyCnt[newP] + happiness[i] > happyCnt[i])                    {                        happyCnt[i] = happyCnt[newP] + happiness[i];                        pre[i] = newP;                        cityCnt[i] = cityCnt[newP] + 1;                    }else if(happyCnt[newP] + happiness[i] == happyCnt[i]){                        if(cityCnt[newP] + 1 < cityCnt[i])                        {                            cityCnt[i] = cityCnt[newP] + 1;                            pre[i] = newP;                        }                    }                }            }        }        int minn = INF;        for(int i=0;i<n;i++)        {            if(!vis[i] && dist[i] < minn)            {                minn = dist[i];                newP = i;            }        }        vis[newP] = true;    }// end while}int main( ){    //freopen("in.txt","r",stdin);    while(cin >> n >> K >> dest)    {        mps2i[dest] = n-1;        mpi2s[n-1] = dest;        d = mps2i[dest];        for(int i=0;i<n;i++)        {            happiness[i] = 0;        }        string city;        int tmp;        for(int i=0;i<n-1;i++)        {            cin >> city >> tmp;            mps2i[city] = i;            mpi2s[i] = city;            happiness[i] = tmp;        }        //         for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)            {                mp[i][j] = -1;            }        }        string c1, c2;        int cost;        for(int i=0;i<K;i++)        {            cin >> c1 >> c2 >> cost;            int ic1 = mps2i[c1];            int ic2 = mps2i[c2];            mp[ic1][ic2] = cost;            mp[ic2][ic1] = cost;        }        Dijkstra();        printf("%d %d %d %d\n", pathCnt[d], dist[d], happyCnt[d], happyCnt[d] / (cityCnt[d] - 1));        vector<string> v;        int vLen = 0;        int fa = d;        while(fa != -1)        {            v.push_back(mpi2s[fa]);            vLen ++;            fa = pre[fa];        }        cout << v[0];        for(int i=1;i<vLen;i++)        {            cout << "->" << v[i];        }        printf("\n");    }    return 0;}
0 0
原创粉丝点击