Usaco/Bessie come home (Dijkstra)

Source: Internet
Author: User

Bessie come home

Go home


It's dinner time, and cows are scattered outside the farm. John the farmer rang the bell, so they started to go to the barn. Your job is to identify which cow will arrive in the Barn first (there will always be and only one of the fastest cows in the given test data ). During milking (before dinner), every cow is on her farm, and some pastures may have no cows. Each farm is connected to one or more farms by one road (may include itself ). Sometimes there is more than one road between two pastures (which may be the same letter. There must be at least one farm and a barn with a road connection. Therefore, all cows can reach the barn at last, and the cows always take the shortest path. Of course, cows can move in any direction at the same speed.
The farm is marked as 'A'... 'Z' and 'A'... 'y'. There is a cow in the farm indicated by uppercase letters, but not in lowercase letters. The tag of the barn is 'Z'. Note that there are no cows in the barn. [Edit] Description


Note that 'M' and 'M' are not the same farm. Otherwise, they are incorrect.The above indicates that the input data may contain both M and M (depressing ing), for example

M a A M Z

Format

Program name: Comehome

Input Format

Row 1st: integer p (1 <= P <= 10000), indicating the number of roads connecting the farm (Barn.

2nd. p + 1 line: two letters separated by space and an integer:

The length of the flag and road connecting to the farm by road (1 <= length <= 1000 ).

Sample Input

(File comehome. In)

5A d 6B d 3C e 9d Z 8e Z 3

Output Format

A separate row contains two items: the marker of the farm where the cows first arrive at the Barn are located, and the length of the path that the cows walk through.

Sample output

(File comehome. out)

B 11
 
There is nothing to say about this question, it is the bare Dijkstra, but since I used to write Dijkstra wrong! And if it weren't for usaco to give data, I wouldn't know that I was wrong... So here we will post the project...
/*ID:138_3531LANG:C++TASK:comehome*/#include <iostream>#include <iomanip>#include <fstream>#include <cmath>#include <cstring>#define MAX 100000using namespace std;int p;int d[130][130];int dist[130];int vis[130];ifstream fin("comehome.in");ofstream fout("comehome.out");void input(){    fin>>p;    for (int i='A';i<='z';i++)        for (int j='A';j<='z';j++)            d[i][j]=MAX;    for (int i='A';i<='z';i++)        dist[i]=MAX;    for (int i=0;i<p;i++)    {        char a,b;        fin>>a>>b;        int temp;        fin>>temp;        if (temp<d[a][b])   {d[a][b]=temp; d[b][a]=temp;}    }}void dijkstra(){    dist['Z']=0;    int ok=1;    while(ok)    {        ok=0;        int min=MAX;        int k;        for (int i='A';i<='z';i++)            if ((!vis[i])&&(dist[i]<min))            {                min=dist[i];                k=i;                ok=1;            }        vis[k]=1;        for (int i='A';i<='z';i++)            if (!vis[i])            {                if (dist[i]>dist[k]+d[i][k])                {                    dist[i]=dist[k]+d[i][k];                }            }    }}int main(){    input();    dijkstra();    int min=MAX;    char k;    for (int i='A';i<'Z';i++)    {        if (i!='Z')            if (dist[i]<min)            {                min=dist[i];                k=i;            }    }    fout<<k<<' '<<min<<endl;    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.