CF 602C The Two Routes(dij+邻接矩阵)

( ̄▽ ̄)"

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdlib>
#include<vector>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;

const int INF=10e8;
const int MAXN=410;

int k,minn;
int lc[MAXN],c1[MAXN][MAXN],c2[MAXN][MAXN];
bool vis[MAXN];

int dij(int n,int c[][MAXN])
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
        lc[i]=c[1][i];
    vis[1]=1;

    for(int j=1;j<=n;j++)
    {
        k=-1,minn=INF;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&minn>lc[i])
            {
                minn=lc[i];
                k=i;
            }
        }
        if(k==-1) break;
        vis[k]=1;
        for(int i=1;i<=n;i++)
            if(!vis[i]&&lc[i]>lc[k]+c[k][i])
                lc[i]=lc[k]+c[k][i];
    }
    if(lc[n]==INF)
        lc[n]=-1;
    return lc[n];
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j)
            {
                c1[i][j]=c1[j][i]=0;
            }
            else
            {
                c1[i][j]=c1[j][i]=INF;
                c2[i][j]=c2[j][i]=INF;
            }
        }
    }

    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        c1[u][v]=c1[v][u]=1;
        if((u==1&&v==n)||(u==n&&v==1))
            flag=1;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(c1[i][j]==0||c1[i][j]==INF)
                c2[i][j]=c2[j][i]=1;
        }
    }
    if(m==n*(n-1)/2)
        printf("-1\n");
    else
    {
        if(flag==1)
            printf("%d\n",dij(n,c2));
        else if(flag==0)
            printf("%d\n",dij(n,c1));
    }
    return 0;
}
时间: 2024-10-10 17:02:34

CF 602C The Two Routes(dij+邻接矩阵)的相关文章

POJ 2387 Til the Cows Come Home(dij+邻接矩阵)

( ̄▽ ̄)" //dijkstra算法: //这题建邻接矩阵的时候有坑(先读入边后读入点),还有重边: #include<iostream> #include<cstdio> using namespace std; const int INF=10e7; const int MAXN=2010; int k,minn; int cost[MAXN][MAXN]; int lowcost[MAXN]; bool vis[MAXN]; void dij(int n,int

POJ 1062 昂贵的聘礼(dij+邻接矩阵)

( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; const int INF=10e8; const int MAXN=110; int k,minn; int c[MAXN][MAXN],lc[MAXN],level[MAXN],X[MAXN]; bool vis[MAXN]; int dij(in

POJ 3268 Silver Cow Party(dij+邻接矩阵)

( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using namespace std; const int INF=10e8; const int MAXN=1010; int k,minn; int cost[MAXN][MAXN],lowcost[MAXN],lc[MAXN]; bool

HDU 1548 A strange lift(dij+邻接矩阵)

( ̄▽ ̄)" //dijkstra算法, //只是有效边(即能从i楼到j楼)的边权都为1(代表次数1): //关于能否到达目标楼层b,只需判断最终lowtime[b]是否等于INF即可. #include<iostream> #include<cstdio> using namespace std; const int INF=10e7; const int MAXN=210; int k,minn; int K[MAXN]; int cost[MAXN][MAXN];

poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置.输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口. 题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出. 1 #in

(中等) CF 576D Flights for Regular Customers (#319 Div1 D题),矩阵快速幂。

In the country there are exactly n cities numbered with positive integers from 1 to n. In each city there is an airport is located. Also, there is the only one airline, which makes m flights. Unfortunately, to use them, you need to be a regular custo

HDU2544 最短路dij

纯最短路. 1 ///HDU 2544堆优化的最短路 2 #include <cstdio> 3 #include <iostream> 4 #include <sstream> 5 #include <cmath> 6 #include <cstring> 7 #include <cstdlib> 8 #include <string> 9 #include <vector> 10 #include <

个性化推荐算法:GRM,CF,NBI的实现

All three algorithms GRM, CF, and NBI can provide each user an ordered queue of all its uncollected movies. For an arbitrary user ui, if the edgeui?ojis in the probe set according to the training set, oj is an uncollected movie for ui, we measure t

cf#261 div2 解题报告

.....代码没什么可说的,主要是学习各路大神姿势 A题 化简化简水题,都告诉平行坐标轴了,数据还出了对角线,后面两个点坐标给的范围也不错 ........和最优代码相比姿势有点混乱 #include <cstdio> int x[4],y[4]; int abs(int n){ return n<0?-n:n; } int main(){ scanf("%d%d%d%d",x,y,x+1,y+1); int dx=abs(x[0]-x[1]); int dy=abs