/ Vijos / 讨论 / 小岛 /

只有15分QAQ

#include<cstdio>
#include<algorithm>
using namespace std;
const int INF=1000000001;
const int maxn=105;
int d[maxn][maxn];
int n,m;
void printdis(int s,int t){
    printf("%d\n",(d[s][t]==INF?-1:d[s][t]));
}
void addedge(int u,int v,int e){
    if(d[u][v]<=e) return;
    d[u][v]=e;
    d[v][u]=e;
    for(int i=1;i<=n;i++){
        if(d[i][u]<INF){
            for(int j=0;j<n;j++){
                if(d[j][v]<INF&&i!=j){
                    d[i][j]=min(d[i][j],d[i][u]+d[u][v]+d[v][j]);
                    d[j][i]=d[i][j];
                }
            }
        }
    }
}
int main(){
    freopen("islet.in","r",stdin);
    freopen("islet.out","w",stdout);
    scanf("%d%d",&n,&m);
    int num,s,t,u,v,e;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
        d[i][j]=(i==j?0:INF);
    while(m--){
        scanf("%d",&num);
        if(!num){
            scanf("%d%d",&s,&t);
            printdis(s,t);
        }
        if(num){
            scanf("%d%d%d",&u,&v,&e);
            addedge(u,v,e);
        }
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}

3 条评论

  • @ 2016-09-21 13:19:36

    对照标答乱搞了一下1楼的程序就AC了QAQ

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int INF=1000000001;
    const int maxn=105;
    int d[maxn][maxn];
    int n,m;
    void printdis(int s,int t){
    printf("%d\n",(d[s][t]==INF?-1:d[s][t]));
    }
    void addedge(int u,int v,int e){
    if(d[u][v]<=e) return;
    d[u][v]=e;
    d[v][u]=e;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++){
    d[i][j]=min(d[i][j],d[u][v]+d[i][u]+d[j][v]);
    d[i][j]=min(d[i][j],d[u][v]+d[i][v]+d[j][u]);
    d[j][i]=d[i][j];
    }
    }
    int main(){
    freopen("islet.in","r",stdin);
    freopen("islet.out","w",stdout);
    scanf("%d%d",&n,&m);
    int num,s,t,u,v,e;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    d[i][j]=(i==j?0:INF);
    while(m--){
    scanf("%d",&num);
    if(!num){
    cin>>s>>t;
    printdis(s,t);
    }
    else {
    cin>>u>>v>>e;
    addedge(u,v,e);
    }
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
    }

  • @ 2016-09-21 13:06:27

    这里是看起来很厉害的标答Q3Q 为什么要松弛2次QAQ

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int INF=1000000000;
    const int maxn=105;
    int n,m;
    int d[maxn][maxn];
    void init(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    d[i][j]=INF;
    for(int i=1;i<=n;i++) d[i][i]=0;
    }
    void relax(int x,int y,int len){
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++){
    if(d[i][x]+d[y][j]+len<d[i][j]) d[i][j]=d[i][x]+d[y][j]+len;
    if(d[i][y]+d[x][j]+len<d[i][j]) d[i][j]=d[i][y]+d[x][j]+len;
    }
    }
    void solve(){
    for(int i=1;i<=m;i++){
    int o,x,y,len;
    cin>>o;
    if(!o){
    cin>>x>>y;
    if(d[x][y]==INF) cout<<"-1\n";
    else cout<<d[x][y]<<"\n";
    }
    else{
    cin>>x>>y>>len;
    relax(x,y,len);
    }
    }
    }
    int main(){
    // freopen("islet.in","r",stdin);
    // freopen("islet.out","w",stdout);
    init();
    solve();
    return 0;
    }

  • @ 2016-09-21 12:48:20

    floyd无脑乱搞O(n^3*m) QAQ

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int INF=1000000001;
    const int maxn=105;
    int d[maxn][maxn];
    int n,m;
    int main(){
    freopen("islet.in","r",stdin);
    // freopen("islet.out","w",stdout);
    scanf("%d%d",&n,&m);
    int num,s,t,u,v,e;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    d[i][j]=(i==j?0:INF);
    while(m--){
    scanf("%d",&num);
    if(!num){
    scanf("%d%d",&s,&t);
    printf("%d\n",(d[s][t]==INF?-1:d[s][t]));
    }
    if(num){
    scanf("%d%d%d",&u,&v,&e);
    d[u][v]=min(d[u][v],e);
    d[v][u]=min(d[u][v],e);
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    d[i][j]=min(d[i][k]+d[k][j],d[i][j]);
    }
    }
    fclose(stdin);
    // fclose(stdout);
    return 0;
    }

  • 1

信息

ID
1942
难度
6
分类
(无)
标签
递交数
690
已通过
188
通过率
27%
被复制
2
上传者