HDU-2544 最短路 水题一枚

       题目:http://acm.hdu.edu.cn/showproblem.php?pid=2544

       水题一枚,纯属练手。

    My code:

 dijstra(优先队列优化)

//STATUS:C++_AC_0MS_320KB
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define LL __int64
#define pii pair
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
const int MAX=110,INF=200000000,MOD=100000007;
const double esp=1e-6;
int dijstra(int s);
int n,m;
int d[MAX],u[MAX*MAX],v[MAX*MAX],first[MAX],next[MAX*MAX],w[MAX*MAX];
int main()
{
//  freopen("in.txt","r",stdin);
    int i,j;
    while(~scanf("%d%d",&n,&m) && (n||m))
    {
        mem(first,-1);
        for(i=0,j=0;i,greater > q;
    for(i=1;i<=n;i++)d[i]=INF;
    d[1]=0;
    u.first=d[1],u.second=1;
    q.push(u);
    while(!q.empty())
    {
        u=q.top();
        q.pop();
        x=u.first,y=u.second;
        if(x==d[y]){
            for(e=first[y];e!=-1;e=next[e]){
                if(d[y]+w[e]

 SPFA:

//STATUS:C++_AC_15MS_316KB
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define LL __int64
#define pii pair
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
const int MAX=110,INF=200000000,MOD=100000007;
const double esp=1e-6;
int SPFA(int s);
int n,m;
int d[MAX],u[MAX*MAX],v[MAX*MAX],first[MAX],next[MAX*MAX],w[MAX*MAX],inq[MAX];
int main()
{
//    freopen("in.txt","r",stdin);
    int i,j;
    while(~scanf("%d%d",&n,&m) && (n||m))
    {
        mem(first,-1);
        for(i=0,j=0;i q;
    mem(inq,0);
    for(i=1;i<=n;i++)d[i]=INF;
    d[1]=0;
    q.push(1);
    inq[1]=1;
    while(!q.empty())
    {
        x=q.front();q.pop();
        inq[x]=0;
        for(e=first[x];e!=-1;e=next[e]){
            if(d[x]+w[e]

你可能感兴趣的:(ACM_最短路,最小生成树)