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

暴力DFS。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=1000+10;
int n,m;
string st;
map<string ,int>m1;
map<int ,string>m2;
int sz;
int h[maxn];
struct Edge
{
    int u,v,c;
}e[maxn*maxn];
int tot;
vector<int>g[maxn];

int des;
int ans_cost=0x7FFFFFFF;
int ans_count=0;
int ans_happy=0;
int ans_point=0;
int path[maxn],ans_path[maxn];
bool flag[maxn];

void dfs(int x,int cost,int happy,int point)
{
    if(cost>ans_cost) return;
    if(x==des)
    {
        if(cost<ans_cost)
        {
            ans_cost=cost;
            ans_count=1;
            ans_happy=happy;
            ans_point=point;
            for(int i=0;i<point;i++)
                ans_path[i]=path[i];
        }

        else if(cost==ans_cost)
        {
            ans_count++;
            if(happy>ans_happy)
            {
                ans_happy=happy;
                ans_point=point;
                for(int i=0;i<point;i++)
                    ans_path[i]=path[i];
            }

            else if(happy==ans_happy)
            {
                if(point<ans_point)
                {
                    ans_point=point;
                    for(int i=0;i<point;i++)
                        ans_path[i]=path[i];
                }
            }
        }
        return;
    }

    for(int i=0;i<g[x].size();i++)
    {
        int id=g[x][i];
        path[point]=e[id].v;
        if(flag[e[id].v]==1) continue;
        flag[e[id].v]=1;
        dfs(e[id].v,cost+e[id].c,happy+h[e[id].v],point+1);
        flag[e[id].v]=0;
    }
}

int main()
{
    scanf("%d%d",&n,&m); cin>>st;
    m1[st]=++sz; m2[sz]=st;

    for(int i=1;i<=n-1;i++)
    {
        string name; cin>>name;
        m1[name]=++sz; m2[sz]=name;
        int val; scanf("%d",&val);
        h[sz]=val;
    }

    des=m1["ROM"];

    tot=0;
    for(int i=1;i<=m;i++)
    {
        string U,V; int c; cin>>U>>V>>c;
        e[tot++].u=m1[U]; e[tot].v=m1[V]; e[tot].c=c;
        g[m1[U]].push_back(tot);

        e[tot++].u=m1[V]; e[tot].v=m1[U]; e[tot].c=c;
        g[m1[V]].push_back(tot);
    }

    memset(flag,0,sizeof flag);
    flag[1]=1;
    dfs(1,0,0,0);

    printf("%d %d %d %d\n",ans_count,ans_cost,ans_happy,ans_happy/ans_point);

    cout<<st;
    for(int i=0;i<ans_point;i++)
        cout<<"->"<<m2[ans_path[i]];
    printf("\n");

    return 0;
}
时间: 2024-10-23 01:26:56

PAT (Advanced Level) 1087. All Roads Lead to Rome (30)的相关文章

PAT (Advanced Level) 1087 All Roads Lead to Rome

题解   最短路径经典题型.套最短路的板子再加上额外的要求就可以了(说起来好简单).SPFA也行,Dijkstra也可以.这里我用的是SPFA.因为题目要求,将地名和其对应的数字用map映射一下,这样方便处理. same[i]代表到达地点 i 有几种路径: dist[i]代表从起点到地点 i 的最短距离: happy[i]代表从起点到地点 i 的幸福值: cnt[i]代表从起点到地点 i 需要经过几个城市: ans[i]代表从哪个地点到达了地点 i : 代码 #include<bits/stdc

Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)

Pat1087代码 题目描述: 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. F

1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise

题目信息 1087. All Roads Lead to Rome (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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. I

[PAT]1087. All Roads Lead to Rome (30)

/************************************************************** 1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different tourist routes from our city to Rome. You are suppos

1087. All Roads Lead to Rome (30)

时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 Spec

pat1087. All Roads Lead to Rome (30)

1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 gain

PAT 1087 All Roads Lead to Rome

1 #include <cstdio> 2 #include <climits> 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 #include <queue> 7 #include <unordered_map> 8 #include <algorithm> 9 10 using namespace std; 11 12 typ

PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=100000+10; struct Node {

PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct Node { int