Floyd && Dijkstra + + adjacent table before the chain to star (explain the true problem Source: City Road)

1381: City Road (Dijkstra)


Time limit: 1000 ms Memory Limit: 65536 KB
Submissions: By 4066 the number: 1163 

Description [title]

Luo was invited to a party, in the city n, and Luo is currently in the city is 1, there are many nearby cities 2 ~ n-1, there is no direct connection between the way some cities, there are between some cities Road directly connected, these roads are two-way, of course, there may be multiple.

Now given directly adjacent to the path length of the city, Luo would like to know from city 1 to city n, the shortest distance number.

[Enter]

Input n, m, and m denotes n urban road;

Next m lines of ABC, showing a city and urban b having a length of the road c.

[Output]

Output Shortest 1 to n. If one does not reach n, -1 is output.

[Sample input]

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100

[Sample Output]

90

【prompt】

[Data] scale and conventions

1≤n≤2000

1≤m≤10000

0≤c≤10000

 

【source】


No

 

Floyd :( been omitted ....)

Dijkstra:

 

#include<bits/stdc++.h>
using namespace std;
int g[2005][2005];
int n,m;
int dis[2005];
bool used[2005];
int main(){
    memset(g,0x3f,sizeof(g));
    cin>>n>>m;
    for(int i=1;i<=m;++i){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        g[a][b]=min(g[a][b],c);
        g[b][a]=min(g[b][a],c);
    }
    memset(dis,0x3f,sizeof(dis));
    dis[1]=0;
    for(int i=1;i<=n;++i){
        int minn=0x3f3f3f3f,minn_j=0;
        for(int j=1;j<=n;++j){
            if(used[j]==false&&dis[j]<minn){
                minn=dis[j];
                minn_j=j;
            }
        }
        if(minn_j==0)
            break;
        used[minn_j]=true;
        for(int j=1;j<=n;j++)
            if(used[j]==false)
                dis[j]=min(dis[j],dis[minn_j]+g[minn_j][j]);
    }
    if(dis[n]==0x3f3f3f3f)
    cout<<-1;
    else
    cout<<dis[n];
}

 

+ Collar connected to the table:

#include<bits/stdc++.h>
using namespace std;
struct node{
    int to,val;
};
vector<node> edge[2005];

int dis[2005];
bool used[2005];
int main(){
    int n,m;
    cin>>n>>m;
    int a,b,c;
    for(int i=1;i<=m;i++){
        cin>>a>>b>>c;
        node t;
        t.to=b;t.val=c;
        edge[a].push_back(t);
        t.to=a;t.val=c;
        edge[b].push_back(t);
    }
    memset(dis,0x3f,sizeof(dis));
    dis[1]=0;
    for(int i=1;i<=n;i++){
         int minn=0x3f3f3f3f,minn_j=0;
        for(int j=1;j<=n;j++){
            if(used[j]==false&&dis[j]<minn){
                minn=dis[j];
                minn_j=j;
            }
        }
        if(minn_j==0)
            break;
        used[minn_j]=true;
        int from=minn_j;
        for(int j=0;j<edge[from].size();j++){
            int to=edge[from][j].to;
            int val=edge[from][j].val;    
            dis[to]=min(dis[to],dis[from]+val);
        }
        
    }
                
    if(dis[n]==0x3f3f3f3f)
        cout<<-1;
    else
        cout<<dis[n];
    
    return 0;
}

+ Before the chain to the star:

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int to,val,next;
};
node edge[20005];

int dis[2005];
bool used[2005];
int head[2005];
int num=0;
void add_edge(int from,int to,int val)
{
    num++;
    edge[num].to=to;
    edge[num].val=val;
    edge[num].next=head[from];
    head[from]=num;
}

int main()
{
    int n,m;
    cin>>n>>m;
    int a,b,c;
    for(int i=1;i<=m;i++)
    {
        cin>>a>>b>>c;
        add_edge(a,b,c);
        add_edge(b,a,c);
    }
    
    memset(dis,0x3f,sizeof(dis));
    memset(used,false,sizeof(used));
    dis[1]=0;
    for(int i=1;i<=n;i++)
    {
        int minn=0x3f3f3f3f,minn_j=0;
        for(int j=1;j<=n;j++)
        {
            if(used[j]==false && dis[j]<minn)
            {
                minn=dis[j];
                minn_j=j;
            }
        }
        if(minn_j==0)
            break;
        used[minn_j]=true;
        int from=minn_j;
        for(int j=head[from];j!=0;j=edge[j].next)
        {
            int to=edge[j].to;
            int val=edge[j].val;
            dis[to]=min(dis[to],dis[from]+val);
        }
    }
                
    if(dis[n]==0x3f3f3f3f)
        cout<<-1;
    else
        cout<<dis[n];
    
    return 0;
}

 

 

 

Guess you like

Origin www.cnblogs.com/crazily/p/10990732.html