Usaco comehome Dijkstra

Source: Internet
Author: User
User: Kevin Samuel [kevin_s1] task: comehomelang: C ++ compiling... compile: okexecuting... test 1: Test OK [0.003 secs, 3376 KB] Test 2: Test OK [0.005 secs, 3376 KB] Test 3: Test OK [0.005 secs, 3376 KB] Test 4: test OK [0.005 secs, 3376 KB] Test 5: Test OK [0.011 secs, 3376 KB] Test 6: Test OK [0.019 secs, 3376 KB] Test 7: test OK [0.035 secs, 3376 KB] Test 8: Test OK [0.005 secs, 3376 KB] test 9: Test OK [0.008 secs, 3376 KB] All tests OK.

Your program ('comehome') worked first time!That's fantastic -- and a rare thing. Please accept these special automatic

Congratulations.

Very easy, it's a shortest path problem

When creating a graph, I directly use A-Z and A-Z as the node to create the graph. I used a simple Dijkstra algorithm to find the nearest point from the Z point reverse.

/*ID:kevin_s1PROG:comehomeLANG:C++*/#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <vector>#include <map>#include <set>#include <algorithm>#include <cstdlib>#include <list>#include <cmath>using namespace std;#define MAXN 53#define INF 99999//gobal variable====int P;int Graph[MAXN][MAXN];int result;int Dist[MAXN];int visited[MAXN];//==================//function==========int Convert(char ch){int index = -1;if(ch >= 'A' && ch <= 'Z'){index = ch - 'A' + 1;}else if(ch >= 'a' && ch <= 'z'){index = ch - 'a' + 27;}elseindex = -1;return index;}int Dijkstra(int v0){int min;int k;//initfor(int i = 1; i < MAXN; i++){visited[i] = false;Dist[i] = Graph[v0][i];}Dist[v0] = 0;visited[v0] = 1;for(int v = 1; v < MAXN; v++){min = INF;for(int w = 1; w < MAXN; w++){if(!visited[w] && (Dist[w] < min)){min = Dist[w];k = w;}}visited[k] = 1;for(int w = 1; w < MAXN; w++){if(!visited[w] && (min + Graph[k][w] < Dist[w])){Dist[w] = min + Graph[k][w];}}}int Min = INF;for(int i = 1; i < MAXN; i++){if(Dist[i] < Min && i >= 1 && i <= 26 && Dist[i] > 0){Min = Dist[i];result = i;}}return Min;}//==================int main(){freopen("comehome.in","r",stdin);freopen("comehome.out","w",stdout);cin>>P;for(int i = 1; i < MAXN; i++){Graph[i][i] = INF;for(int j = 1; j < MAXN; j++){Graph[i][j] = INF;}}memset(visited, 0, sizeof(visited));while(P--){char chst, chen;int dist;cin>>chst>>chen>>dist;int st = Convert(chst);int en = Convert(chen);if(dist < Graph[st][en]){Graph[st][en] = dist;Graph[en][st] = dist;}}int ans = Dijkstra(26);char chfarm = 'A' + result - 1;cout<<chfarm<<" ";cout<<ans<<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.