ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1806. Мобильные телеграфы

Please help me to avoid TLE#8
Послано Tigran Hakobyan[RAU] 31 дек 2011 17:11
Code :
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <iterator>
#include <functional>
#include <utility>
#include <ctime>
#include <string>

using namespace std;
#define MAXN 50000
#define INF 1000000000
int n;
int d[10];
long long int a[MAXN];
int D[MAXN];
int P[MAXN];
vector < pair < int, int > > g[MAXN];
long long tenP[] = {1LL, 10LL, 100LL, 1000LL, 10000LL, 100000LL, 1000000LL, 10000000LL, 100000000LL, 1000000000LL};
int sendCost (int i, int j) {
    int cnt = 0;
    int len = 0;
    int valueI1 = -1, valueJ1 = -1;
    int valueI2 = -1, valueJ2 = -1;
    for(int k = 0; k < 10; ++k) {
        int p = (a[i] / tenP[9 - k]) % 10;
        int q = (a[j] / tenP[9 - k]) % 10;
        if(p != q) {
            ++cnt;
            if(valueI1 == -1 && valueJ1 == -1) {
                valueI1 = p;
                valueJ1 = q;
            }
            else {
                valueI2 = p;
                valueJ2 = q;
            }
        }
        if(p == q && cnt == 0) {
            len = len + 1;
        }
    }
    if(cnt > 2) {
        return -1;
    }
    if(cnt == 2) {
        if(((valueI1 == valueJ2) & (valueI2 == valueJ1)) == 0) {
            return -1;
        }
    }
    return d[len];
}
int dist(int start, int finish) {
    D[start] = 0;
    priority_queue < pair < int, int > > q;
    q.push(make_pair(0, start));
    while(!q.empty()) {
        int v = q.top().second;
        int cur_d = -q.top().first;
        q.pop();
        if(cur_d > D[v]) {
            continue;
        }
        for(int i = 0; i < g[v].size(); ++i) {
            int to = g[v][i].first;
            int len = g[v][i].second;
            if(D[v] + len < D[to]) {
                D[to] = D[v] + len;
                P[to] = v;
                q.push(make_pair(-D[to], to));
            }
        }
    }
    return D[finish];
}
int main () {
    scanf("%d", &n);
    for(int i = 0; i < 10; ++i) {
        scanf("%d", d + i);
    }
    for(int i = 0; i < n; ++i) {
        D[i] = INF;
        P[i] = -1;
        scanf("%lld", a + i);
    }
    for(int i = 0; i < n; ++i) {
        for(int j = i + 1; j < n; ++j) {
            int curr = sendCost(i, j);
            if(curr != -1) {
                g[i].push_back(make_pair(j, curr));
                g[j].push_back(make_pair(i, curr));
            }
        }
    }
    int ans = dist(0, n - 1);
    printf("%d\n", ans == INF ? -1 : ans);
    if(ans == INF) {
        return 0;
    }
    stack < int > st;
    int start = n - 1;
    while(start != -1) {
        st.push(start);
        start = P[start];
    }
    printf("%d\n", st.size());
    while(!st.empty()) {
        int k = st.top();
        st.pop();
        printf("%d ",k + 1);
    }
    return 0;
}
Re: Please help me to avoid TLE#8
Послано radical 9 авг 2015 13:00
you spend a lot of time creating graphs.
change a method
focus on this sentence
A message can be sent from a telegraph a to a telegraph b only if the number b can be obtained from the number a by changing exactly one digit or by swapping two digits,
think about the telegraph's digit
try to change every digit one time  or swap two digit
you can use map<long long,int>
Re: Please help me to avoid TLE#8
Послано Dima_int15 9 авг 2015 19:20
Хватит матюкатся