1087. All Roads Lead to Rome (30)

来源:互联网 发布:淘宝热销商品分析 编辑:程序博客网 时间:2024/06/06 08:38

一直觉得自己dijkstra算法已经比较熟了。
遇到这种变化的题目还是很难靠自己写出来。
唉,没有天分学算法真是慢啊。。。
参考了http://blog.csdn.net/qq_26437925/article/details/47683767这位大佬的算法。
写的很清晰,能读懂。

#include <bits/stdc++.h>using namespace std;map<string, int> change;map<int, string> rchange;vector<int> happy(201);int n;bool visit[201];int pre[201];int way[201][201];int dis[201];int allhappy[201], happyave[201];int roadnum[201], nodenum[201];void disjktra(int start, int end){    nodenum[start] = 0;    int i, tempv, j, min;    for (i = 0; i < n; i++)    {        dis[i] = INT_MAX;        allhappy[i] = happy[i];        roadnum[i] = 1;    }    visit[start] = true;    dis[start] = 0;    tempv = start;    while (tempv != end)    {        for (i = 0; i < n; i++)        {            if (!visit[i] && way[tempv][i])            {                if (dis[tempv] + way[tempv][i] < dis[i])                {                    pre[i] = tempv;                    allhappy[i] = allhappy[tempv] + happy[i];                    dis[i] = dis[tempv] + way[tempv][i];                    nodenum[i] = nodenum[tempv] + 1;                    happyave[i] = allhappy[i] / nodenum[i];                    roadnum[i] = roadnum[tempv];                }                else if (dis[tempv] + way[tempv][i] == dis[i])                {                    roadnum[i] += roadnum[tempv];                    if (allhappy[i] < happy[i] + allhappy[tempv])                    {                        pre[i] = tempv;                        allhappy[i] = allhappy[tempv] + happy[i];                        nodenum[i] = nodenum[tempv] + 1;                        happyave[i] = allhappy[i] / nodenum[i];                    }                    else if(allhappy[i] == happy[i] + allhappy[tempv])                    {                        int tmp = nodenum[tempv] + 1;                        if (tmp < nodenum[i])                        {                            pre[i] = tempv;                            allhappy[i] = allhappy[tempv] + happy[i];                            nodenum[i] = nodenum[tempv] + 1;                            happyave[i] = allhappy[i] / nodenum[i];                        }                    }                }            }        }        int mi = INT_MAX;        for (j = 0; j < n; j++)        {            if (!visit[j] && dis[j] < mi)            {                mi = dis[j];                tempv = j;            }        }        visit[tempv] = true;    }}int main(void){    int road, index = 1;    string start;    cin >> n >> road >> start;    change[start] = 0;    rchange[0] = start;    happy[0] = 0;    int i;    for (i = 0; i < n - 1; i++)    {        string temp;        int tmp;        cin >> temp >> tmp;        change[temp] = index;        rchange[index] = temp;        happy[index] = tmp;        index++;    }    for (i = 0; i < road; i++)    {        string temp1, temp2;        int len;        cin >> temp1 >> temp2 >> len;        int t1 = change[temp1], t2 = change[temp2];        way[t1][t2] = way[t2][t1] = len;    }    int end = change["ROM"];    disjktra(0, end);    printf("%d %d %d %d\n", roadnum[end], dis[end], allhappy[end], happyave[end]);    cout << start;    stack<int> sup;    sup.push(end);    while (pre[end] != 0)    {        end = pre[end];        sup.push(end);    }    while (!sup.empty())    {        cout << "->" << rchange[sup.top()];        sup.pop();    }}

这题主要考察目标就是disjktra算法的变化使用。
网上也有用dfs的,不过dfs时间复杂度太高,还是能用disjktra就用disjkrtra吧。
这类只要找到一条最优路的题目还是非常适合用disjktra去解决的