团体程序设计天梯赛-练习集 - L3-011 直捣黄龙(30 分)

简介: 团体程序设计天梯赛-练习集 - L3-011 直捣黄龙(30 分)

题目链接:点击打开链接

题目大意:略。

解题思路:注意判断条件时的次序严格性的问题。

AC 代码

#include<bits/stdc++.h>#include<cmath>#define mem(a,b) memset(a,b,sizeof a)#define ssclr(ss) ss.clear(), ss.str("")#define INF 0x3f3f3f3f#define MOD 1000000007usingnamespacestd;
typedeflonglongll;
constintmaxn=250;
unordered_map<string,int>id;
unordered_map<int,string>name;
intn,m;
intvis[maxn], path[maxn], pre[maxn], cst[maxn], num[maxn], cost[maxn], dis[maxn];
intg[maxn][maxn];
voiddijkstra(ints)
{
dis[s]=0, path[s]=1;
while(1)
    {
intmi=INF; s=-1;
for(inti=1;i<=n;i++)
if(!vis[i] &&mi>dis[i]) mi=dis[i], s=i;
if(s==-1) return;
vis[s]=1;
for(inti=1;i<=n;i++)
        {
if(!vis[i] &&mi+g[s][i]<dis[i])
            {
dis[i]=mi+g[s][i];
pre[i]=s;
num[i]=num[s]+1;
path[i]=path[s];
cst[i]=cst[s]+cost[i];
            }
elseif(!vis[i] &&mi+g[s][i]==dis[i])
            {
path[i]+=path[s];
if(num[s]+1>num[i])
                {
pre[i]=s;
num[i]=num[s]+1;
cst[i]=cst[s]+cost[i];
                }
elseif(num[s]+1==num[i])
                {
if(cst[i]<cst[s]+cost[i])
                    {
pre[i]=s;
cst[i]=cst[s]+cost[i];
                    }
                }
            }
//            else if(!vis[i] && mi+g[s][i]==dis[i] && num[s]+1==num[i]) // 不能并列,需要放到第二条里面//            {//                path[i]+=path[s];//                if(cst[i]<cst[s]+cost[i])//                {//                    pre[i]=s;//                    cst[i]=cst[s]+cost[i];//                }//            }        }
    }
}
intmain()
{
mem(pre,-1), mem(g,INF), mem(dis,INF);
intth=0,w,u,v;
strings,d,ts,us,vs;
scanf("%d%d",&n,&m);
cin>>s>>d;
id[s]=++th, name[th]=s;
for(inti=1;i<n;i++)
    {
cin>>ts>>w;
id[ts]=++th, name[th]=ts;
cost[th]=w;
    }
for(inti=0;i<m;i++)
    {
cin>>us>>vs>>w;
g[id[us]][id[vs]]=g[id[vs]][id[us]]=w;
    }
dijkstra(id[s]);
vector<int>vec;
inth=id[d];
while(h!=-1)
    {
vec.push_back(h);
h=pre[h];
    }
for(inti=vec.size()-1;i>=0;i--)
printf("%s%s",name[vec[i]].c_str(),i==0?"\n":"->");
printf("%d %d %d\n",path[id[d]],dis[id[d]],cst[id[d]]);
return0;
}
目录
相关文章
|
12月前
团体程序设计天梯赛-练习集L2篇⑨
团体程序设计天梯赛-练习集L2篇⑨
140 0
|
12月前
团体程序设计天梯赛-练习集L2篇⑦
团体程序设计天梯赛-练习集L2篇⑦
58 0
|
12月前
|
Perl
团体程序设计天梯赛-练习集L1篇③
团体程序设计天梯赛-练习集L1篇③
115 0
|
12月前
|
测试技术
团体程序设计天梯赛-练习集L2篇⑥
团体程序设计天梯赛-练习集L2篇⑥
81 0
|
算法 安全 定位技术
团体程序设计天梯赛(上)
团体程序设计天梯赛
409 0
团体程序设计天梯赛(上)
|
存储 大数据
团体程序设计天梯赛(下)
团体程序设计天梯赛(下)
373 0
团体程序设计天梯赛(下)
|
算法
团体程序设计天梯赛-模拟赛(下)
团体程序设计天梯赛-模拟赛(下)
446 0
团体程序设计天梯赛-模拟赛(下)
|
机器学习/深度学习 程序员 Python
团体程序设计天梯赛-模拟赛(上)
团体程序设计天梯赛-模拟赛
661 0
团体程序设计天梯赛-模拟赛(上)
|
人工智能
【CCCC】PAT : 团体程序设计天梯赛-练习集 L1 答案
【CCCC】PAT : 团体程序设计天梯赛-练习集 L1 答案
320 0
【CCCC】PAT : 团体程序设计天梯赛-练习集 L3 答案(01-23)
【CCCC】PAT : 团体程序设计天梯赛-练习集 L3 答案(01-23)
111 0