The most short-circuit hdu-4522 ghost series story-coming home

Source: Internet
Author: User

Question link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4522

Question meaning:

Find the minimum discomfort between the known start point and the known end point.

Note that if a route is set to 1, you can select either a hard seat or a sleeper.

Solution:

Dijela algorithm.

Code:

 

# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <stack> # include <list> # include <queue> # include <ctype. h> # define EPS 1e-6 # define Inf (1 <30) # define PI ACOs (-1.0) using namespace STD; # define maxn 207 bool edge [maxn] [maxn] [2]; // edge [I] [J] [0] indicates that a hard seat can be used between I and j, 1 indicates that a sleeper can be set to int vis [maxn] [2], DIS [maxn] [2], a [2]; char Save [10000 + 7]; int N; void dijs (INT start, int kind) {memset (VIS, false, sizeof (VIS); For (INT I = 1; I <= N; I ++) {If (edge [start] [I] [kind]) dis [I] [kind] = A [kind]; else dis [I] [kind] = inf;} dis [start] [kind] = 0; vis [start] [kind] = true; For (INT I = 1; I <= n-1; I ++) {int min = inf, flag; For (Int J = 1; j <= N; j ++) {If (vis [J] [kind] = false) {If (DIS [J] [kind] <min) {min = dis [J] [kind]; flag = J ;}}if (min = inf) return; vis [fla G] [kind] = true; For (int K = 1; k <= N; k ++) {If (vis [k] [kind]) continue; if (edge [flag] [k] [kind] & A [kind] + min <dis [k] [kind]) dis [k] [kind] = A [kind] + min ;}} int main () {int Q, T, K, start, end; scanf ("% d", & Q); While (Q --) {scanf ("% d", & N, & T); memset (edge, false, sizeof (edge); For (Int J = 1; j <= T; j ++) {scanf ("% S % d", save, & K ); int Len = strlen (SAVE); save [Len ++] = '\ 0'; int last = 0, cur = 0; int I = 0; (; save [I]! = '+'; I ++) Last = last * 10 + save [I]-'0'; for (I ++; I <Len; I ++) {If (! Isdigit (save [I]) {edge [last] [cur] [k] = true; If (k) edge [last] [cur] [0] = true; last = cur; // printf ("% d \ n", cur); cur = 0 ;} else cur = cur * 10 + save [I]-'0';} scanf ("% d", & A [0], & A [1]); scanf ("% d", & START, & End); dijs (START, 0); // dijs (START, 1) in case of hard seat ); // case of sleeper int ans = min (DIS [end] [0], DIS [end] [1]); If (ANS = inf) printf ("-1 \ n"); else printf ("% d \ n", ANS);} 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.