Oj2894 (bellman Ford template)

Source: Internet
Author: User

Http://acm.sdut.edu.cn/sdutoj/problem.php? Action = showproblem & problemid = 2894

Because of carelessness, I didn't have a at noon. The question is "2000000 edge undirected graphs", but I opened 2000000 in the array. It's a damn thing. When I saw the status of others' A, the memory was twice as large as mine, I instantly knew that my hand was broken, obviously 4000000,

But what I don't understand is that spfa is an improved version of be, but why is it 7100 Ms.

# Include <iostream> # include <stdio. h> # include <string. h> # include <stdlib. h> # define n 10000001 using namespace STD; struct node {int X, Y, W;} edge [4000002]; int dis [500002]; int n, m, S, e; int T, flag; void add (INT X1, int Y1, int W1) {edge [T]. X = x1; edge [T]. y = Y1; edge [t ++]. W = W1;} void B () {for (INT I = 0; I <= N; I ++) dis [I] = N; DIS [s] = 0; for (INT I = 1; I <= n-1; I ++) {flag = 0; For (Int J = 0; j <t; j ++) {If (DIS [edge [J]. x] + edge [J]. W <dis [edge [J]. y]) {flag = 1; DIS [edge [J]. y] = dis [edge [J]. x] + edge [J]. W ;}} if (flag = 0) break;} printf ("% d \ n", DIS [e]);} int main () {int XX, YY, ZZ; while (scanf ("% d", & N, & M )! = EOF) {T = 0; while (M --) {scanf ("% d", & XX, & YY, & zz); add (XX, YY, ZZ); add (YY, XX, ZZ);} scanf ("% d", & S, & E); B ();} return 0 ;} /************************************ Problem ID: sdut OJ 2894 User name: zlh130205 result: accepted take Memory: 49344 K take time: 710 Ms submit time: 13:32:27 **************************************/

 

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.