zoj 2504

感觉最近好像思路好像迟钝好多。。

/*
zoj_2504    最短路
简单题
题意:
这题的题意就是john上学,她妈妈给他规定的有路线,但是只有第一条路必须
按照他妈妈的规定的路线,其他的可以自己掌握,问看看有没有比她妈妈说的
路线更短的线路,如果有,则输出抄近道节省下来的时间,如果不能到达,就
输出N;

以上为别人解释的题意,其实我觉得这题叙述有问题。。而且题目很不严密。。
不想深究。
*/
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
using namespace std;
int map[1010][1010];
bool flag[1010];
struct node
{
    int id,value;
    node( int x=0,int y=0 ) { id=x,value=y; }
};

bool operator<( const node&a,const node&b )
{
    return a.value>b.value;
}

void inint()
{
    int i,j;
    for( i=0;i<1010;i++ )
        for( j=0;j<1010;j++ )
            map[i][j]=-1;
    memset( flag,0,sizeof(flag) );
}

int dijkstra( int sta,int n,int v )
{
    int i;
    priority_queue <node>pq;
    node temp;
    if( v==-1 ) return -1;
    pq.push( node(sta,v) );
    while( !pq.empty() )
    {
        temp=pq.top();  pq.pop();
        if( temp.id==n )    return temp.value;
        flag[temp.id]=1;
        for( i=2;i<=n;i++ )
            if( map[temp.id][i]!=-1 && !flag[i] )
                pq.push( node( i,temp.value+map[temp.id][i] ) );
    }
    return -1;
}

int main()
{
    int T,n,m,k,i,j;
    int a,b,v,sum,sta,count,value;
    scanf( "%d",&T );
    count=1;
    while( T-- )
    {
        inint();
        scanf( "%d%d",&n,&m );
        for( i=0;i<m;i++ )
        {
            scanf( "%d%d%d",&a,&b,&v );
            map[b][a]=map[a][b]=v;
        }
        scanf( "%d%d",&k,&a );
        sum=0;
        for( i=1;i<k;i++ )
        {
            scanf( "%d",&b );
            if( i==1 )  sta=b ,value=map[1][b];
            sum+=map[a][b];
            a=b;
        }
        value=dijkstra( sta,n,value );
        if( value==-1 ) printf( "TEST %d N\n",count );
        else printf( "TEST %d Y %d\n",count,sum-value );
        count++;
    }
    return 0;
}


你可能感兴趣的:(zoj 2504)