HUN 11532 Tourin【优先队列+Dijkstra】


 

Problem description
   The best friends Mr. Li and Mr. Liu are touring in beautiful country M.
   M has n cities and m two-way roads in total. Each road connects two cities with fixed length.We assume that the cost of car traveling on the road is only related to the length of road,the longer road the more money to pay.
   Now,both Mr. Li and Mr. Liu are in the city C,they have chosen to travel separately by the next time.
   Mr. Li chooses city A with beautiful scenery to be next place, Mr. Liu goes to city B with ancient temples.
   You are their friend with clever minds,just tell them how to arrive the target places make total costs of them minimum.


Input
   The input file contains sevearl test cases.The first line of each case are two positive integers n,and m(3≤n≤5000, 1≤m≤10000). The cities are named from 1 to n.Three positive integers C, A, B are follwing.Then,m lines are given,each line contains three integers i,j and k,indicating one road between i and j is exists,and should pay cost k by the car.
   Process to the end of file.

Output
   For each test case, first print a line saying "Scenario #p", where p is the number of the test case.Then,if both Mr. Li and Mr. Liu can manage to arrive their cities,output the minimum cost they will spend,otherwise output "Can not reach!", in one line.Print a blank line after each test case, even after the last one.

Sample Input
4 5
1 3 4
1 2 100
1 3 200
1 4 300
2 3 50
2 4 100
4 6
1 3 4
1 2 100
1 3 200
1 4 300
2 3 50
2 4 100
3 4 50
Sample Output
Scenario #1
250

Scenario #2
200

code:

View Code
#include<stdio.h>
#include<string.h>
#include<queue>
#define INF 0x1f1f1f1f
using namespace std;
int head[10005];
struct node
{
    int to,w,next;
}q[20020];
int n,tot;
void add(int s,int u,int wi)
{
    q[tot].to=u;
    q[tot].w=wi;
    q[tot].next=head[s];
    head[s]=tot++;
}
struct dd
{
    int xu,di;
    bool operator < (dd t)const{
        return t.di<di;
    }
}tt,in;
priority_queue<dd>dis;
void dijkstra(int u,int*d)
{
    int i;

    in.xu=u;
    in.di=0;
    d[u]=0;
    dis.push(in);
    while(!dis.empty())
    {
        tt=dis.top();
        dis.pop();

        for(i=head[tt.xu];i;i=q[i].next)
        {
            in.xu=q[i].to;
            in.di=tt.di+q[i].w;
            if(in.di<d[in.xu])
            {
                d[in.xu]=in.di;
                dis.push(in);
            }
        }
    }
}
int d1[10005],d2[10005],d3[10005];
int main()
{
    int m;
    int A,B,C,a,b,c,i,res,ca=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<=n;i++)
        head[i]=0;
        tot=1;
        scanf("%d%d%d",&C,&A,&B);
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        for(i=0;i<=n;i++)
            d1[i]=INF;
        dijkstra(C,d1);
        printf("Scenario #%d\n",ca++);
        if(d1[A]==INF||d1[B]==INF)
        {
            printf("Can not reah!\n\n");
            continue;
        }
        for(i=0;i<=n;i++)
        d2[i]=d3[i]=INF;
        dijkstra(A,d2);
        dijkstra(B,d3);
        res=INF;
        for(i=1;i<=n;i++)
        if(d1[i]+d2[i]+d3[i]<res)
        res=d1[i]+d2[i]+d3[i];
        printf("%d\n\n",res);
    }
    return 0;
}
posted @ 2012-04-12 14:08  'wind  阅读(241)  评论(0编辑  收藏  举报