CSU 1312 CX and girls (Shortest Path)

Source: Internet
Author: User
1321: Cx and girlstime limit: 1 sec memory limit: 128 MB
Submit: 432 solved: 124
[Submit] [Status] [web board] Description

CX is to rush to class. In order not to be late, it is necessary to arrive at the classroom in the shortest path. At the same time, CX hopes to see more students on the way, the better. Now, the map is abstracted into an undirected graph. CX starts from, and the classroom is at, telling the number of school girls at each point and the length of each edge. Find the maximum number of students that can be seen in the classroom in the shortest path of Cx.

Input

Multiple groups of input data (up to 20 groups), input to the end of the file.

There are two positive integers n in the first row of each group of data, where n represents the number of points (2 <= n <= 1000 ), m represents the number of edges (1 <= m <= 10000 ).

N numbers in the next row, representing 1 ~ N The number of School Sisters on each node (0 <= Ni <= 50 ). In the next m row, each row contains three numbers a B c (1 <= A, B <= N, 0 <C <= 100), which indicates that A and B have edges and the length is C. (Duplicate edges may exist)

Output

Output the maximum number of students that can be seen when CX ranges from 1 to n at the shortest distance. If the number of students cannot reach the value of N output-1.

Sample Input
4 41 2 3 41 2 11 3 12 4 23 4 2
Sample output
8
Hint

 

Csu_zzy

 

Source

CSU monthly 2013 Oct.

 

Here, the shortest path is used, but the weight value is added. Find the shortest path and output the number of sisters. If there are multiple shortest paths, the one with the most sisters is output.

 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 using namespace std; 7 const int INF=0x3f3f3f3f; 8 const int MAXN=1000+20; 9 int w[MAXN][MAXN],dis[MAXN],num[MAXN],a[MAXN],vis[MAXN];10 int n,m;11 void dijkstra()12 {13     memset(vis,0,sizeof(vis));14     for(int i=1;i<=n;i++)15         dis[i]=INF;16 17     dis[1]=0;18     a[1]=num[1];19     for(int i=1;i<=n;i++)20     {21         int x,temp=INF;22         for(int j=1;j<=n;j++)23         {24             if(!vis[j]&&dis[j]<=temp)25             {26                 x=j;27                 temp=dis[j];28             }29         }30         vis[x]=1;31         for(int k=1;k<=n;k++)32         {33             if(dis[k]>dis[x]+w[x][k])34             {35                 dis[k]=dis[x]+w[x][k];36                 a[k]=a[x]+num[k];37             }38             else if(dis[k]==dis[x]+w[x][k]&&a[k]<a[x]+num[k])39                 a[k]=a[x]+num[k];40         }41     }42 }43 int main()44 {45     //freopen("in.txt","r",stdin);46     while(scanf("%d %d",&n,&m)!=EOF)47     {48         for(int i=1;i<=n;i++)49             scanf("%d",&num[i]);50 51         for(int i=1;i<=n;i++)52             for(int j=1;j<=n;j++)53                 w[i][j]=INF;54 55         for(int i=1;i<=m;i++)56         {57             int star,en,val;58             scanf("%d %d %d",&star,&en,&val);59             if(val<w[star][en])60                 w[star][en]=w[en][star]=val;61         }62 63         dijkstra();64 65         if(dis[n]==INF)66             printf("-1\n");67         else68             printf("%d\n",a[n]);69     }70     return 0;71 }
View code

 

CSU 1312 CX and girls (Shortest Path)

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.