結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー 👑 kmjpkmjp
提出日時 2021-01-22 23:20:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,741 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 211,436 KB
実行使用メモリ 135,284 KB
最終ジャッジ日時 2023-08-27 22:33:56
合計ジャッジ時間 11,779 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 41 ms
114,004 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 88 ms
119,616 KB
testcase_24 AC 73 ms
118,232 KB
testcase_25 AC 175 ms
125,500 KB
testcase_26 AC 259 ms
130,948 KB
testcase_27 AC 180 ms
127,740 KB
testcase_28 AC 128 ms
122,356 KB
testcase_29 AC 168 ms
127,664 KB
testcase_30 AC 127 ms
122,604 KB
testcase_31 AC 101 ms
120,696 KB
testcase_32 AC 128 ms
125,508 KB
testcase_33 AC 231 ms
129,892 KB
testcase_34 AC 240 ms
129,408 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 120 ms
124,640 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 110 ms
129,624 KB
testcase_46 WA -
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M;
vector<vector<ll>> E[202020];
vector<int> RE[202020];
int reach[202020];
class SCC {
public:
	static const int MV = 2025000;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<NV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0,i; SC.clear(); SC.resize(NV); NUM.clear();
		assert(NV);
		FOR(i,NV) vis[i]=0; FOR(i,NV) if(!vis[i]) dfs(i); FOR(i,NV) vis[i]=0;
		for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};
SCC scc;

ll num[202020];
ll sum[202020];
int in[202020];
const ll mo=1000000007;
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	scc.init(N+1);
	FOR(i,M) {
		cin>>x>>y>>j>>l;
		E[x].push_back({y,j,l});
		RE[y].push_back(x);
		scc.add_edge(x,y);
	}
	queue<int> Q;
	reach[N]=1;
	Q.push(N);
	while(Q.size()) {
		x=Q.front();
		Q.pop();
		FORR(e,RE[x]) if(reach[e]==0) {
			reach[e]=1;
			Q.push(e);
		}
	}
		cout<<"INF"<<endl;
		return;
	scc.scc();
	FOR(i,N) if(reach[i]&&scc.SC[scc.GR[i]].size()>1) {
		cout<<"INF"<<endl;
		return;
	}
	if(reach[0]==0) return _P("0\n");
	
	FOR(i,N+1) {
		FORR(e,E[i]) if(reach[i]&&reach[e[0]]) in[e[0]]++;
	}
	
	FOR(i,N+1) if(reach[i]&&in[i]==0) Q.push(i);
	num[0]=1;
	sum[0]=0;
	
	while(Q.size()) {
		x=Q.front();
		Q.pop();
		FORR(e,E[x]) if(reach[e[0]]) {
			(num[e[0]]+=num[x]*e[2])%=mo;
			(sum[e[0]]+=e[2]*(sum[x]+num[x]*e[1]%mo))%=mo;
			in[e[0]]--;
			if(in[e[0]]==0) Q.push(e[0]);
		}
	}
	
	cout<<sum[N]<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0