#include<bits/stdc++.h>
using namespace std;
map<string,int>city;
map<int,string>rcity;
map<int,vector<pair<int,int> > >edge;//对比string要比对比int慢很多,所以转换映射
int dis[207],path[207],hcount[207],happ[207],fstep[207],f[207];//源点到各点的最短距离,最短路径数量,快乐总数,快乐,经过点的个数,前一个点
int vis[207];
int main(){
    memset(dis,-1,sizeof(dis));
    memset(hcount,-1,sizeof(hcount));
    std::ios::sync_with_stdio(false);//关闭同步
    int n,k,i,d,s;
    string st,u,v;
    cin>>n>>k>>st;
    city[st]=0;//编号
    rcity[0]=st;//反编号
    happ[0]=0;
    dis[0]=0;
    hcount[0]=0;
    fstep[0]=0;
    path[0]=1;//init
    f[0]=0;
    for(i=1;i<n;++i){
        f[i]=i;
        cin>>u;
        rcity[i]=u;
        city[u]=i;
        cin>>happ[i];
    }
    for(i=0;i<k;++i){
        cin>>u>>v>>d;
        edge[city[u]].push_back(make_pair(city[v],d));//建邻接表
        edge[city[v]].push_back(make_pair(city[u],d));
    }
    s=0;
    vector<pair<int,int> >::iterator it;
    int next;
    while(s!=city["ROM"]){
        vis[s]=1;
        for(it=edge[s].begin();it!=edge[s].end();++it){
            next=it->first;
            if(dis[next]==-1||dis[next]>dis[s]+it->second){//松弛
                dis[next]=dis[s]+it->second;
                hcount[next]=hcount[s]+happ[next];
                path[next]=path[s];
                fstep[next]=fstep[s]+1;
                f[next]=s;
            }
            else{
                if(dis[next]==dis[s]+it->second){
                    path[next]+=path[s];
                    if(hcount[next]<hcount[s]+happ[next]){
                        hcount[next]=hcount[s]+happ[next];
                        fstep[next]=fstep[s]+1;
                        f[next]=s;
                    }
                    else{
                        if(hcount[next]==hcount[s]+happ[next]){
                            if(fstep[next]>fstep[s]+1){
                                fstep[next]=fstep[s]+1;
                                f[next]=s;
                            }
                        }
                    }
                }
            }
        }
        int mindis=-1,minnum;
        for(i=1;i<n;i++){
            if(dis[i]==-1)//如果当前边到不了初始点,直接pass
                continue;
            if(!vis[i]&&(mindis==-1||(dis[i]<mindis))){
                mindis=dis[i];
                minnum=i;
            }
        }
        s=minnum;//找到当前距离源点最近的一个点向下搜索
    }
    cout<<path[s]<<" "<<dis[s]<<" "<<hcount[s]<<" "<<hcount[s]/fstep[s]<<endl;
    int p=s;
    stack<int>ss;
    while(p){
        ss.push(p);
        p=f[p];
    }
    cout<<rcity[p];
    while(!ss.empty()){
        cout<<"->"<<rcity[ss.top()];
        ss.pop();
    }
    return 0;
}

/*#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9 ;
typedef struct node{
    int v;//节点编号
    int cost;//边权值
};
int N;//城市数量
int K;//道路条数
string start;//起始点城市名字
int happy[207];//存放各个城市的快乐值
map<string,int>stringToInt;//城市名字->编号
map<int,string>intToString;//编号->城市名字
vector<node>graph[207];//邻接表
int d[207];//记录从起始城市到达点i的最少花费值
vector<int>pre[207];//记录前一个节点
vector<int>path,tempPath;
int optHappy=0;
double optAverageHappy=0;
int cnt=0;//记录最短路径条数
int inq[207];//记录节点是否在队列中
void spfa(int s){
    for(int i=0;i<N;++i)
        d[i]=INF;
    d[s]=0;
    queue<int>q;
    q.push(s);
    inq[s]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        inq[u]=0;
        for(int j=0;j<graph[u].size();++j){
            int v=graph[u][j].v;
            int cost=graph[u][j].cost;
            if(!v)
                continue;
            if(d[u]+cost<d[v]){
                d[v]=d[u]+cost;
                pre[v].clear();
                pre[v].push_back(u);
                if(!inq[v]){
                    q.push(v);
                    inq[v]=1;
                }
            }
            else if(d[u]+cost==d[v])
                pre[v].push_back(u);
        }
    }
}
void dfs(int nowVisit){
    if(!nowVisit){
        cnt++;
        tempPath.push_back(nowVisit);
        int happyValue=0;
        for(int i=tempPath.size()-2;i>=0;--i)
            happyValue+= happy[tempPath[i]];
        double averageHappyValue=1.0*happyValue/(tempPath.size()-1);
        if(happyValue>optHappy){
            optHappy=happyValue;
            optAverageHappy=averageHappyValue;
            path=tempPath;
        }
        else if(happyValue==optHappy&&averageHappyValue>optAverageHappy){
            optAverageHappy=averageHappyValue;
            path=tempPath;
        }
        tempPath.pop_back();
        return;
    }
    tempPath.push_back(nowVisit);
    for(auto&it:pre[nowVisit])
  dfs(it);
    tempPath.pop_back();
}
int main(){
    std::ios::sync_with_stdio(false);//关闭同步
    cin>>N>>K>>start;
    stringToInt[start]=0; //起始城市编号为0
    intToString[0]=start;
    string city;
    int happyness;
    for(int i=1;i<N;++i){
        cin>>city>>happyness;
        stringToInt[city]=i;
        intToString[i]=city;
        happy[i]=happyness;
    }
    string city1,city2;
    int cost;
    for(int i=0;i<K;++i){
        cin>>city1>>city2>>cost;
        int id1=stringToInt[city1];
        int id2=stringToInt[city2];
        node x,y;
        x.v=id2;
        x.cost=cost;
        y.v=id1;
        y.cost=cost;
        graph[id1].push_back(x);
        graph[id2].push_back(y);
    }
    int destination=stringToInt["ROM"];
    spfa(0);
    dfs(destination);
    cout<<cnt<<" "<<d[destination]<<" "<<optHappy<<" "<<(int)optAverageHappy<<endl;
    for(int i=path.size()-1;i>=0;--i) {
        cout<<intToString[path[i]];
        if(i)
            cout<<"->";
    }
    return 0;
}*/

//SPFA先处理再DFS

All Roads Lead to Rome(30)(MAP【int,string】,邻接表,DFS,模拟,SPFA)(PAT甲级)的更多相关文章

  1. [图的遍历&多标准] 1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) Indeed there are many different tourist routes from our city to Ro ...

  2. pat1087. All Roads Lead to Rome (30)

    1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  3. 1087. All Roads Lead to Rome (30)

    时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different ...

  4. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  5. PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

    暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  6. 1087 All Roads Lead to Rome (30)(30 分)

    Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...

  7. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  8. PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]

    1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...

  9. PAT_A1087#All Roads Lead to Rome

    Source: PAT A1087 All Roads Lead to Rome (30 分) Description: Indeed there are many different tourist ...

随机推荐

  1. BZOJ 1656 [Usaco2006 Jan] The Grove 树木:bfs【射线法】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1656 题意: 给你一个n*m的地图,'.'表示空地,'X'表示树林,'*'表示起点. 所有 ...

  2. js 跨域复习 window.name | window.domain | iframe | Jsonp

    引起跨域的原因: 浏览器的同源策略,但是当你要发送请求的时候,出于安全性问题,浏览器有严格的要求,必须协议,域名,端口都相同,这个就是同源策略. 影响:a通过js脚本向b发送ajax请求,不同源就会报 ...

  3. jQuery 对表单、表格的操作及更多应用-简略笔记

    [jQuery 对表单.表格的操作及更多应用] jquery对表单及表格的操作是实际应用中相当广泛. 对于表单的操作,可以实现 (1)获取和失去焦点改变样式: (2)在多行文本框中可以实现网站评论框的 ...

  4. django admin扩展user表password验证及set_password

    一般如果扩展了django user内置表,在admin后台创建新用户的时候密码将会变成明文,故而导致登录不成功.所以我们在admin.py可以通过form自定义进行对password进行操作,可以双 ...

  5. Java丨验证码图片去除干扰像素,方便验证码的识别

    1.先来看看效果: 原图 除去干扰像素后 2.解析代码: 1).读取文件夹里面的图片 String fileName = "picture"; BufferedImage img ...

  6. Hibernate - POJO 类和数据库的映射文件*.hbm.xml

    POJO 类和关系数据库之间的映射可以用一个XML文档来定义. 通过 POJO 类的数据库映射文件,Hibernate可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据库表列之间的对 ...

  7. 【LeetCode】036. Valid Sudoku

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  8. 在oracle中,select语句查询字段中非纯数字值

    最近,将原来的数字符字段转换为数字时,总报错误:无效数字. 如何找出其中哪些是非数字字符的记录?比较麻烦的事.下面是用Oracle DB自带的函数translate可以找出来的 1.创建测试表 Cre ...

  9. BZOJ1067:[SCOI2007]降雨量

    浅谈\(RMQ\):https://www.cnblogs.com/AKMer/p/10128219.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...

  10. 1.4 isAlive()方法

    方法isAlive()是判断当前线程是否处于活动状态. 线程代码: public class TestThread extends Thread{ @Override public void run( ...