栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3613 Cow Relays

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3613 Cow Relays

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 205int n,t,st,en;struct Mat{      int mat[maxn][maxn];      void init()    {        memset(mat,-1,sizeof(mat));    }};  int v[1005];//离散化用Mat ans,tmp,map;int cnt;//顶点个数void init(){    int val,s,e;    memset(v,0,sizeof(v));    cnt=0;    ans.init();    tmp.init();    map.init();    for(int i=0;i<maxn;i++)        ans.mat[i][i]=0;    for(int i=0;i<t;i++)    {        scanf("%d%d%d",&val,&s,&e);        if(v[s]==0)        { ++cnt; v[s]=cnt;        }        if(v[e]==0)        { ++cnt; v[e]=cnt;        }        if(map.mat[v[s]][v[e]]<0 || map.mat[v[s]][v[e]] > val) map.mat[v[s]][v[e]]=map.mat[v[e]][v[s]]=val;    }}void floyd(Mat b,Mat c){    tmp.init();    for(int k=1;k<=cnt;k++)        for(int i=1;i<=cnt;i++) for(int j=1;j<=cnt;j++) {         if(b.mat[i][k] <0 || c.mat[k][j] <0)//意味着是inf         continue;     if(tmp.mat[i][j]<0 || tmp.mat[i][j] > b.mat[i][k] + c.mat[k][j]  )          tmp.mat[i][j] = b.mat[i][k] + c.mat[k][j]; }}Mat copy(){    Mat a;    for(int i=0;i<=cnt;i++)        for(int j=0;j<=cnt;j++) a.mat[i][j]=tmp.mat[i][j];    return a;}void solve(){    while(n)    {        if(n&1)        { floyd(ans,map); ans=copy();        }        floyd(map,map);        map=copy();        n>>=1;    }}int main(){    while(scanf("%d%d%d%d",&n,&t,&st,&en) != EOF)    {        init();        solve();        printf("%dn",ans.mat[v[st]][v[en]]);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373856.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号