zoukankan      html  css  js  c++  java
  • 洛谷P1629 邮递员送信

    题目描述

    有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N。由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间。这个邮递员每次只能带一样东西。求送完这N-1样东西并且最终回到邮局最少需要多少时间。

    输入输出格式

    输入格式:

    第一行包括两个整数N和M。

    第2到第M+1行,每行三个数字U、V、W,表示从A到B有一条需要W时间的道路。 满足1<=U,V<=N,1<=W<=10000,输入保证任意两点都能互相到达。

    【数据规模】

    对于30%的数据,有1≤N≤200;

    对于100%的数据,有1≤N≤1000,1≤M≤100000。

    输出格式:

    输出仅一行,包含一个整数,为最少需要的时间。

    输入输出样例

    输入样例#1:
    5 10
    2 3 5
    1 5 5
    3 5 6
    1 2 8
    1 3 8
    5 3 4
    4 1 8
    4 5 3
    3 5 6
    5 4 2
    输出样例#1:
    83

    单源最短路。先跑一边Dijkstra求1到各点的最短路,再将所有边反向,Dij求各点到1的最短路

     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<vector>
     8 #include<queue>
     9 using namespace std;
    10 const int mxn=100010;
    11 int read(){
    12     int x=0,f=1;char ch=getchar();
    13     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    14     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    15     return x*f;
    16 }
    17 int n,m;
    18 struct exp{
    19     int x,y,w;
    20 }ex[mxn];
    21 struct edge{
    22     int v,nxt,dis;
    23 }e[mxn<<1];
    24 int hd[mxn],mct=0;
    25 void add_edge(int u,int v,int d){
    26     e[++mct].v=v;e[mct].nxt=hd[u];e[mct].dis=d;hd[u]=mct;
    27     return;
    28 }
    29 struct node{
    30     int v,d;
    31     bool operator < (const node &r) const {return d>r.d;}
    32 };
    33 priority_queue<node>tp;
    34 int dis[mxn];
    35 int mdis[mxn];
    36 void Dij(){
    37     memset(dis,0x3f,sizeof dis);
    38     while(!tp.empty()) tp.pop();
    39     tp.push((node){1,0});
    40     dis[1]=0;
    41     while(!tp.empty()){
    42         node tmp=tp.top();
    43         tp.pop();
    44         if(tmp.d>dis[tmp.v])continue;
    45         int u=tmp.v;
    46         for(int i=hd[u];i;i=e[i].nxt){
    47             int v=e[i].v;
    48             if(dis[v]>dis[u]+e[i].dis){
    49                 dis[v]=dis[u]+e[i].dis;
    50                 tp.push((node){v,dis[v]});
    51             }
    52         }
    53     }
    54     return;
    55 }
    56 int main(){
    57     n=read();m=read();
    58     int i,j;
    59     for(i=1;i<=m;i++){
    60         ex[i].x=read();ex[i].y=read();
    61         ex[i].w=read();
    62         add_edge(ex[i].x,ex[i].y,ex[i].w);
    63     }
    64     Dij();
    65     memcpy(mdis,dis,sizeof dis);
    66     memset(hd,0,sizeof hd);
    67     mct=0;
    68     for(i=1;i<=m;i++)add_edge(ex[i].y,ex[i].x,ex[i].w);
    69     Dij();
    70     int ans=0;
    71     for(int i=2;i<=n;i++){
    72         ans+=dis[i]+mdis[i];
    73     }
    74     printf("%d
    ",ans);
    75     return 0;
    76 }
  • 相关阅读:
    理解Python闭包,这应该是最好的例子
    2021-01-31
    论unity中UI工具与GUI函数
    2021-01-31
    第八届“图灵杯”NEUQ-ACM程序设计竞赛(全题解&&详细)
    第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛(同步赛)全题解
    Go-快速排序
    网络地址转换NAT原理及其作用
    解析私有IP地址和公网IP地址
    first blog
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6065853.html
Copyright © 2011-2022 走看看