Uva(10986)

很裸的一道dijk算法题,因为顶点数太多无法用邻接矩阵表示,所以要用临界表来表示

AC代码

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
using namespace std;
const int maxn=2*50000;
#define inf 99999999
struct node
{
    int v;
    int u;
    int w;
    int next;
}V[maxn];
int head[maxn];
int d[maxn];
int n,m,s,t;
int tol;
int done[maxn];
typedef pair<int,int>pii;
priority_queue<pii,vector<pii>,greater<pii> >q;
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void dijk()
{
    memset(done,0,sizeof(done));
    for(int i=0;i<=n;i++)
    d[i]=inf;
    d[s]=0;
    q.push(make_pair(d[s],s));
    while(!q.empty())
    {
      pii  u=q.top();
      q.pop();
      int x=u.second;
      if(done[x])
      continue;
      done[x]=1;
      for(int e=head[x];e!=-1;e=V[e].next)
      {
         int v=V[e].v;
         int w=V[e].w;
         if(d[x]+w<d[v])
         {
             d[v]=d[x]+w;
             q.push(make_pair(d[v],v));
         }
      }
   }
      if(d[t]==inf)
      printf("unreachable\n");
      else
      printf("%d\n",d[t]);
}
int main()
{
    int T;
    int tmp=0;
    scanf("%d",&T);
    while(T--)
    {
    scanf("%d %d %d %d",&n,&m,&s,&t);
    init();
    m*=2;
    int u,v,w;
    for(int i=0;i<m;i+=2)
    {
        scanf("%d %d %d",&u,&v,&w);
        V[i].u=u;
        V[i].v=v;
        V[i].w=w;
        V[i].next=head[u];
        head[u]=i;
        V[i+1].u=v;
        V[i+1].v=u;
        V[i+1].next=head[v];
        V[i+1].w=w;
        head[v]=i+1;
    }
     printf("Case #%d: ",++tmp);
    dijk();
   }
   return 0;
} 
时间: 2024-12-17 06:23:53

Uva(10986)的相关文章

UVA 10986 Sending email(SPFA)

There are n SMTP servers connected by network cables. Each of the m cables connects two computers and has a certain latency measured in milliseconds required to send an email message. What is the shortest time required to send a message from server S

uva 10986 Sending email (dijkstra)

uva 10986 Sending email "A new internet watchdog is creating a stir in Springfield. Mr. X, if that is his real name, has come up with a sensational scoop."Kent Brockman There are n SMTP servers connected by network cables. Each of the m cables c

UVA 10986 Sending email 【dijkstra + 堆优化】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1927 题意:n个点m条边,求s到e的最短距离 代码: #include<stdio.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<cty

UVa 10986 - Sending email

链接:http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1927 //邻接表+队列 #include <iostream> #include <string.h> #include <queue> #include <stdio.h> using namespac

UVA 10986 Sending email 最短路问题

基本的最短路问题 就是数据需要稍微处理一下.(N比较大)dijkstra也要优化.不优化应该会T: #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #i

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED