码迷,mamicode.com
首页 > 其他好文 > 详细

HNU 13375 Flowery Trails (最短路)

时间:2015-08-09 20:51:06      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:算法   图论   dijkstar   

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);
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HNU 13375 Flowery Trails (最短路)

标签:算法   图论   dijkstar   

原文地址:http://blog.csdn.net/u010372095/article/details/47378403

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!