幾道最短路裸題–解法dij

來源:互聯網
上載者:User

講座裡講了好多最短路的演算法,沒來得及思考和應用因為目前碰到的水題,dij就完全可以解決了,目前的目標是昨天聽到的dij的二元堆積最佳化,研究成功會第一時間放到這裡。

還有spfa可以解決負權的問題,由於學長是曾經研究過劉汝佳的演算法入門經典的,他十分注重一個模型,DAG。

第一道hdu1548

A strange lift

#include<iostream>#include<cstdio>#include<cstring>#define max_n 300#define maxn 99999using namespace std;int dist[max_n],map[max_n][max_n];int vis[max_n],n;void dijk(int v){    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)        dist[i]=map[v][i];    dist[v]=0;    vis[v]=1;    for(i=1;i<=n;i++)    {        int m=maxn,f=0;        for(int j=1;j<=n;j++)            if(!vis[j]&&m>dist[j])            {                m=dist[j];                f=j;            }        if(f==0)            break;        vis[f]=1;        for(j=1;j<=n;j++)            if(!vis[j] && dist[j]>dist[f]+map[f][j])                dist[j]=dist[f]+map[f][j];    }}int button[max_n];int main(){    int a,b,i,j;    while(scanf("%d%d%d",&n,&a,&b)!=EOF && n)    {        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)                map[i][j]=maxn;        for(i=1;i<=n;i++)        {            scanf("%d",&button[i]);            if(i+button[i]<=n)                map[i][button[i]+i]=1;            if(i-button[i]>=1)                map[i][i-button[i]]=1;        }        dijk(a);        if(dist[b]==maxn)            printf("-1\n");        else            printf("%d\n",dist[b]);    }    return 0;}        

第二道hdu 3790

最短路徑問題
#include<stdio.h>#include<string.h>#define max_n 1010#define maxn 99999struct node{    int l,p;};    node dist[max_n],map[max_n][max_n];int vis[max_n],n;void dijk(int v){    int i,j;    memset(vis,0,sizeof(vis));    for(i=1;i<=n;i++)    {        dist[i].l=map[v][i].l;        dist[i].p=map[v][i].p;        //dist[i].l=maxn;        //dist[i].p=maxn;    }    dist[v].l=0;    dist[v].p=0;    vis[v]=1;    for(i=1;i<=n;i++)    {        int m=maxn,f=0;        for(j=1;j<=n;j++)            if(!vis[j] && m>dist[j].l)            {                m=dist[j].l;                f=j;            }        if(f==0)            break;        vis[f]=1;        for(j=1;j<=n;j++)        {            if(!vis[j] && dist[j].l>dist[f].l+map[f][j].l)            {                dist[j].l=dist[f].l+map[f][j].l;                dist[j].p=dist[f].p+map[f][j].p;            }            if(!vis[j] && dist[j].l==dist[f].l+map[f][j].l &&dist[j].p>dist[f].p+map[f][j].p)                dist[j].p=dist[f].p+map[f][j].p;        }    }}int main(){    int a,b,i,j,m,s,t,c,d;    while(scanf("%d%d",&n,&m)!=EOF &&n &&m)    {        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)            {                map[i][j].l=maxn;                map[i][j].p=maxn;            }        for(i=1;i<=m;i++)        {            scanf("%d%d%d%d",&a,&b,&c,&d);            if(map[a][b].l>c)            {                map[a][b].l=map[b][a].l=c;                map[a][b].p=map[b][a].p=d;            }            if(map[a][b].l==c && map[a][b].p>d)            {                map[a][b].p=map[b][a].p=d;            }        }        scanf("%d%d",&s,&t);        dijk(s);        int ans=dist[t].l;        int ans1=dist[t].p;        printf("%d %d\n",ans,ans1);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.