HNU 13375 Flowery Trails (最短路)

Flowery Trails
Time Limit: 50000ms, Special Time Limit:125000ms, Memory Limit:65536KB
Total submit users: 23, Accepted users: 21
Problem 13375 : No special judgement
Problem description



Input

The first line of the input has two integers: P and T. P is the number of points of interest and T is the number of trails. Points are identified by integers, ranging from 0 to P-1. The entrance point is 0 and the highest peak is point P-1.
Each of the following T lines characterises a different trail. It contains three integers, p1, p2, and l, which indicate that the (two-way) trail links directly points p1 and p2 (not necessarily distinct) and has length l (in metres). Integers in the same line are separated by a single space.
2<=P<=10 000 Number of points.
1<=T<=250 000 Number of trails.
1<=l<=1 000 Length of a trail.


Output

The output has a single line with the extent of flowers (in metres) needed to cover both sides of the popular trails.


Sample Input
10 15
0 1 580
1 4 90
1 4 90
4 9 250
4 2 510
2 7 600
7 3 200
3 3 380
3 0 150
0 3 100
7 8 500
7 9 620
9 6 510
6 5 145
5 9 160
4 7
0 1 1
0 2 2
0 3 10
0 3 3
1 3 2
2 3 1
1 1 1
Sample Output
3860
18
Problem Source
HNU Contest 

题意:把属于最短路上的所有边长相加*2就是答案。

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int N = 10005;
#define INF 1<<30
#define ll long long
struct EDG{
    int to,next;
    ll cost;
}edg[8000000] , node[5000000];
struct NODE{
    int u;
    ll cost;
    friend bool operator<(NODE aa,NODE bb){
        return aa.cost>bb.cost;
    }
};
int eid,head[N];
ll disS[N],disT[N];
void init(int n){
    eid=0;
    for(int i=0; i<=n; i++)
        disS[i]=disT[i]=INF;
    memset(head,-1,sizeof(head));
}
void addEdg(int u,int v,ll cost)
{
    edg[eid].to=v; edg[eid].next=head[u]; edg[eid].cost=cost; head[u]=eid++;
    edg[eid].to=u; edg[eid].next=head[v]; edg[eid].cost=cost; head[v]=eid++;
}
bool vist[N];
void dijkstar(int flag,int n)
{
    priority_queue<NODE>q;
    NODE now,pre;
    memset(vist,0,sizeof(vist));
    if(flag==0)
        now.u=0 , disS[0]=0;
    else
        now.u=n-1 , disT[n-1]=0;
    now.cost=0;
    q.push( now );
    while( !q.empty()){
        pre=q.top(); q.pop();
        if(vist[pre.u])continue;
        vist[pre.u]=1;
        for(int i=head[pre.u]; i!=-1; i=edg[i].next){
            int v=edg[i].to;

            if(flag==0){
                if(disS[v]>pre.cost+edg[i].cost){
                    disS[v]=pre.cost+edg[i].cost;
                    now.u=v;
                    now.cost=disS[v];
                    q.push(now);
                }
            }
            else{
                if(disT[v]>pre.cost+edg[i].cost){
                    disT[v]=pre.cost+edg[i].cost;
                    now.u=v;
                    now.cost=disT[v];
                    q.push(now);
                }
            }
        }
    }
}
int main()
{
    int n,m , u,v;
    ll cost , ans;
    while(scanf("%d%d",&n,&m)>0)
    {
        init(n);
        for(int i=0; i<m; i++){
            scanf("%d%d%lld",&u,&v,&cost);
            node[i].to=u;
            node[i].next=v;
            node[i].cost=cost;
            addEdg( u , v, cost);
        }
        dijkstar(0 , n);
        dijkstar(1 , n);
        ll mindis=disS[n-1] , ans=0;
        for(int i=0; i<m; i++)
        {
            u=node[i].to;
            v=node[i].next;
            if(disS[u]+node[i].cost+disT[v]==mindis||disS[v]+node[i].cost+disT[u]==mindis)
                ans+=node[i].cost;
        }
        printf("%lld\n",ans*2);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值