AtCoder Regular Contest 102 - D - All Your Paths are Different Lengths
题意:
构造出一个带边权有向图,使得从 1 到 N 恰好有 L 条路径,且路径长度从 0 到 L-1
其中顶点数 N <= 20 , 边数 M <= 60
边有向,有权值,可以重边。
如果每相邻两个点之间连两条边一条为0一条为2^i这可得到所有的0~2^k-1
但是在 2^k-1 ~ L 这段区间就不能在直接加上 2^(k+1)-1这样就不能满足恰好L条路径的条件
而且接下来加的边的权值必须要比之前所能达到的最大数要大
然后就是类似一个二进制转换的过程
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int n, cnt = 0, a[62][3], tot = 0;
int main()
{
scanf("%d", &n); n--;
int ed = 1, x = 1;
for(int i=1;i<20 && n;i++,x<<=1) {
if(x > n) break;
a[++cnt][0] = i; a[cnt][1] = i+1; a[cnt][2] = x;
a[++cnt][0] = i; a[cnt][1] = i+1; a[cnt][2] = 0;
n -= x; tot += x; ed = i+1;
}
if(n) {
for(int i=ed; i>=1; i--) {
while(1<<(i-1) <= n) {
n -= (1<<(i-1));
a[++cnt][0] = i; a[cnt][1] = ed; a[cnt][2] = tot + 1;
tot = tot + (1<<(i-1));
}
}
}
printf("%d %d\n", ed, cnt);
for(int i=1;i<=cnt;i++) printf("%d %d %d\n", a[i][0], a[i][1], a[i][2]);
return 0;
}