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