Shortest path algorithm Codevs 1557 heat wave

Source: Internet
Author: User
Tags time limit

Time limit: 1 s space limit: 256000 KB title level: Diamonds Diamond

Title Description Description
The simple people of Texas are suffering a huge heat wave this summer ... Their Texas horned cows are good to eat, but they are not very good at producing cream-rich dairy products. Farmer John at this time first, enjoy spirit, Shenxianshizu to Texas to carry a large number of nutritious cold milk, to alleviate the Texan people endure the pain of heat.
FJ has studied the route of transporting milk from Wisconsin to Texas. These routes include a starting point and an end point that are preceded by a total of T (1 <= t <= 2,500) towns, conveniently labeled 1 to T. In addition to the beginning and end of the field each town is connected by two bidirectional roads to at least two other towns. Each road has a pass fee (including fuel, toll, etc.).
Given a map, the roads containing C (1 <= C <= 6,200) are directly connected to 2 towns. Each road consists of the beginning of the road RS, the end point re (1 <= Rs <= t; 1 <= Re <= t), and the cost (1 <= Ci <= 1,000). The minimum total cost for the town Te (1 <= te <= t) from the beginning of the town TS (1 <= TS <= t) to the end point.
Enter a description input Description

First line: 4 integers separated by a space: T, C, Ts, Te

2nd to C+1: line i+1 describes article I road. There are 3 integers separated by a space: Rs, RE and CI
Outputs description Output Description

A single integer represents the minimum total cost from TS to TE. The data guarantees that there is at least one road.
Sample input to sample

7 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1
Sample outputs sample output
7
Data size & Hint
5->6->1->4 (3 + 1 + 3)

Dijkstra algorithm
#include <iostream>
using namespace std;
const int maxn=999999;
int t,c,ts,te;
int dis[10000],a[2510][2510];
int X,y,z,k,minn;
BOOL flag[10000];
int main ()
{
    cin>>t>>c>>ts>>te;
    for (int i=1;i<=c;i++)
    {
        cin>>x>>y>>z;
        A[x][y]=a[y][x]=z;
        DIS[I]=MAXN;
    }
    dis[ts]=0;
    for (int i=1;i<=t;i++)
    {
        minn=maxn;
        k=0;
        for (int j=1;j<=t;j++)
        if (!flag[j]&&dis[j]<minn)
        {
            minn=dis[j];
            k=j;
        }
        if (k==0) break;
        Flag[k]=true;
        for (int j=1;j<=t;j++)
        if (!flag[j]&&a[k][j]&&dis[j]>dis[k]+a[k][j])
        dis[j]=dis[k ]+A[K][J];
    }
    cout<<dis[te];
    return 0;
}

http://blog.csdn.net/cax1165/article/details/51812502
There are many ways to get to the website

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.