The graph of data structure experiment seven: The best short Circuit of the Donkey Friend program minimum cost __ Shortest

Source: Internet
Author: User
graph of data structure experiment seven: The Donkey friend plan Time limit:1000ms Memory limit:65536k Topic DescriptionAs a senior donkey friend, small new has a collection of self-driving tour map, the figure detailed on the national highway between the city distance and highway charges, now you write a program to find a way to the shortest path between the destination, if there are more than one path shortest, then the output of the least travel path. input

Continuous T-Group data entry, the first row of each set of input data gives four positive integer n,m,s,d, where n (2 <= n <= 500) is the number of cities, the city number from 0~n-1,m is the number of urban expressway, S is the city number of departure, D is the city number of the destination; then M line  , each line gives a highway information, said City 1, City 2, highway length, charge amount, the middle by space interval, the number is integer and not more than 500, the input data are guaranteed to have the solution. Output

Output path length and total charges in the same row, with space spacing between the data. Sample Input

1
4 5 0 3 0 1 1 1 3 2 0 3 4 0 2 2 2
Sample Output
3 40
Tips
#include <bits/stdc++.h> using namespace std;
const int inf=20000000;
const int maxn=1000;
int MAP[MAXN][MAXN];
int CON[MAXN][MAXN];
int n,m,s,d; int Floyd () {for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {for (int k=0;k<n;k + +) {if ((map[j][k]>map[j][i]+map[i][k)) | |
                (Map[j][k]==map[j][i]+map[i][k]&&con[j][k]>con[j][i]+con[i][k])
                    {Map[j][k]=map[j][i]+map[i][k];
                CON[J][K]=CON[J][I]+CON[I][K];
}} printf ("%d%d\n", map[s][d],con[s][d]);
    int main () {int T;
            while (~SCANF ("%d", &t)) {while (t--) {scanf ("%d%d%d%d", &n,&m,&s,&d);
                    for (int i=0;i<=n;i++) {for (int j=0;j<=n;j++) {
                    if (i==j) map[i][j]=con[i][j]=0;
          Else          Map[i][j]=con[i][j]=inf;
                } for (int i=0;i<m;i++) {int u,v,w,x;
                scanf ("%d%d%d%d", &u,&v,&w,&x);
                     if (map[u][v]>w) {map[u][v]=map[v][u]=w;
                    Con[u][v]=con[v][u]=x;
        } Floyd ();
} return 0;
 }

#include <bits/stdc++.h> using namespace std;
const int inf=100000000;
const int maxn=1000000;
int n,m,s,d,lsum=0,msum=0;
struct node {int l,m,u,v;} EDGE[MAXN];
int DIS[MAXN],CON[MAXN];
        int Bellman () {for (int i=0; i<=n; i++) {dis[i]=inf;
    Con[i]=inf;
    } dis[s]=0;
    con[s]=0; for (int i=1; i<n; i++) {for (int j=0; j<2*m; J + +) {if (edge[j). L+DIS[EDGE[J].U]&LT;DIS[EDGE[J].V]) | | ((EDGE[J). L+DIS[EDGE[J].U]==DIS[EDGE[J].V]) && (Edge[j]. M+CON[EDGE[J].U]&LT;CON[EDGE[J].V]))) {Dis[edge[j].v]=edge[j].
                L+DIS[EDGE[J].U]; CON[EDGE[J].V]=CON[EDGE[J].U]+EDGE[J].
            M
} printf ("%d%d\n", dis[d],con[d]);
    int main () {int t,v,u,w,x;
            while (~SCANF ("%d", &t)) {while (t--) {scanf ("%d%d%d%d", &n,&m,&s,&d);
            int num=0;
    for (int i=0; i<m; i++) {            scanf ("%d%d%d%d", &u,&v,&w,&x); Edge[num].
                L=w; Edge[num].
                M=x;
                Edge[num].u=u;
                 Edge[num++].v=v; Edge[num].
                L=w; Edge[num].
                M=x;
                Edge[num].v=u;
            Edge[num++].u=v;
        } bellman ();
} 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.