hdu3790-我可以再坑点吗

把p[i][j],写成g[i][j],结果wa了十次!!我可以再坑点吗?!

这题要注意重边情况的处理,还有重边时cost的更新(不过据说数据没有卡到cost更新上)。还有就是在dijkstra上更新时要注意分==和<的情况!

再附上一组数据,方便检查:

5 7
1 2 5 5
2 3 4 5
1 3 4 6
3 4 2 2
3 5 4 7
4 5 2 4
1 3 4 4
1 5
0 0

答案是8 10;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1005;
int g[maxn][maxn];
int vis[maxn],dis[maxn],p[maxn][maxn],cost[maxn];

void init(int n)
{
    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
            p[i][j]=g[i][j]=INF;
    for(int i=0;i<=n;i++)p[i][i]=g[i][i]=0;
}

void dijkstra(int from,int to,int n)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++){
        dis[i]=g[from][i];
        cost[i]=p[from][i];
    }
    cost[from]=dis[from]=0;
    vis[from]=1;
    for(int i=1;i<n;i++){
        int x,MIN=INF;
        for(int y=1;y<=n;y++)
            if(!vis[y]&&dis[y]<MIN)
                MIN=dis[x=y];
        //if(MIN==INF)return ;
        vis[x]=1;
        for(int y=1;y<=n;y++){
            if(!vis[y]&&g[x][y]<INF){
                int len=dis[x]+g[x][y],w=cost[x]+p[x][y];
                if(dis[y]==len){
                    if(cost[y]>w)cost[y]=w;
                }
                else if(dis[y]>len){
                    cost[y]=w;
                    dis[y]=len;
                }
            }
        }
    }
    printf("%d %d\n",dis[to],cost[to]);
}

int main()
{
    //freopen("int.txt","r",stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m)&&(n||m)){
        init(n);
        int x,y,w,l;
        for(int i=0;i<m;i++){
            scanf("%d%d%d%d",&x,&y,&w,&l);
            if(w<g[x][y]){
                g[y][x]=g[x][y]=w;
                p[y][x]=p[x][y]=l;
            }
            else if(w==g[x][y]){
                if(l<p[x][y]){
                    p[y][x]=p[x][y]=l;
                }
            }
        }
        scanf("%d%d",&x,&y);
        dijkstra(x,y,n);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值