首先你要知道你測試點文件的位置,然后你要創建一個接收結果的文件,將你代碼中的std::cin替換成infile,std::cout替換成outfile即可
#include <fstream>
int main() {// 打開輸入文件std::ifstream infile("C:\\Users\\Downloads\\P4779_1.in");// 打開輸出文件std::ofstream outfile("C:\\Users\\Desktop\\臨時.txt");int n, m, s;// 從輸入文件中讀取數據infile >> n >> m >> s;tsort t(n);while (m--) {int x, y, v;infile >> x >> y >> v;t.add(x, y, v);}t.all(s,0,0);t.all(0,0,s);for (int i = 1; i <= n; i++) {// 將輸出結果寫入到輸出文件中outfile << t.dp[i] << ' ';}// 關閉文件infile.close();outfile.close();return 0;
}